1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Fundamentals of Software Development 1Slide 1 Review: Why start with WordGames? You wrote your first lines of code in this course in WordGames.You wrote.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
18 File handling1June File handling CE : Fundamental Programming Techniques.
Classes and Objects in Java
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Java Classes Using Java Classes Introduction to UML.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Lecture 2: Classes and Objects, using Scanner and String.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Unit Testing CSIS 3701: Advanced Object Oriented Programming.
ITI 1120 Lab #11 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
CS12230 Introduction to Programming Extra example– More on responsibilities 1.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.
ITI 1120 Lab #10 Objects and Classes Slides by: Romelia Plesa, Sylvia Boyd, Alan Williams, Diana InkPen, Daniel Amyot, Gilbert Arbez, Mohamad Eid.
CSC111 Quick Revision.
Building Java Programs
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
CS305J Introduction to Computing
Lecture Note Set 1 Thursday 12-May-05
Computer Programming Methodology Input and While Loop
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Something about Java Introduction to Problem Solving and Programming 1.
BIT115: Introduction to Programming
Building Java Programs
Building Java Programs
Building Java Programs
CSC 113 Tutorial QUIZ I.
Building Java Programs
Lec 4: while loop and do-while loop
Building Java Programs
slides created by Ethan Apter
Week 4 Lecture-2 Chapter 6 (Methods).
Building Java Programs
Building Java Programs
CSS161: Fundamentals of Computing
Objects with ArrayLists as Attributes
More on iterations using
Presentation transcript:

1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things

1. Reading from the keyboard There are a few ways – we will use Scanner Essentials are: import java.util.*; ….. Scanner in=new Scanner(System.in); ……. System.out.print("enter license: "); license=in.nextLine(); ….. System.out.print("enter year: "); year=in.nextInt(); in.nextLine();//after a nextInt() 2

Scanner is a builtin java class Check out the ‘API’ at: This looks like the documentation we had for BJBeetle (called javadoc) – and it is 3

2. Main program A main responsibility is to ‘run’ a whole software system This is done from a special method associated with the class not the objects (static): public static void main(String args[]) { //Fill in in a minute } 5

Suppose we have a Clock with 2 Times alarm time myClock 6 hrs 8 mins 30 hrs 10 mins 21

Here is the Class diagram (code in 2-week2-clock) code on next slide – just look at column 1 first Clock Time alarm -Time time -boolean isSet Clock() +setTime(int h, int m) +setAT(int h, int m) +void turnOnAlarm() +void turnOffAlarm() +boolean isAlarmRinging() //too hard +String toString() Time int hrs -int mins Time () +void setTime(int h, int m) +String toString()

8

public class Time{ private int hrs; private int mins; public Time() { hrs=1; mins=0; } public void setTime(int h, int m) { hrs=h; mins=m; } public String toString() { return hrs+" : "+mins; } public class Clock{ //horrible formatting to fit private Time alarm; private Time time; boolean isSet; public Clock(){ alarm=new Time(); time=new Time(); isSet=false;} public setTime(int h, int m){ time.setTime(h,m); } public setAT(int h, int m){ alarm.setTime(h,m); } public void turnOnAlarm(){ isSet=true; } public void turnOffAlarm(){ isSet=true;} public String toString(){ if (isSet) { return "time is "+time.toString()+" and alarm is set to "+alarm.toString(); } else { return "time is "+time.toString()+" ad alarm is off"; } }

Mess around with the Time and Clock classes You can exercise the methods Using a UseThis class you can even put those method calls in a main() but it is all a bit ad- hoc Instead we create an Application type of class and then run it in a main Look at RunClock.java 10

Do that – create a RunClock object (code next) And then call its runApplication() method Now put these two things in the main: public static void main(String args[]) { RunClock clock=new RunClock(); clock.runApplication(); }

12

Now when you run the main You are running those two methods! You can run from the command line with java RunClock (incidentally you can bypass BlueJ by typing javac *.java to compile) You can actually have a class with nothing in it but a main (see Main.java) or put the main in the application class (RunClock for this example) It is good practice to have a small main program

Note – shows up under class static things belong to the class

Not ‘event-driven’ programming A loop asks the user what to do The user answers This is NOT … ….event-driven, which sits passively and waits until the user does something (GUIs usually are) We’ll do that next term

3. Responsibilities Our first examples were mostly about storing information – methods used are: constructors, gets, sets, toString, readKeyboard That’s fine but we need more! The runApplication() method started to show you how objects have responsibilities to DO something More next week on this and main programs 17

How do you know? What instance variables to put in? think about what each object of that class HAS every clock has 2 times and is either set or not Complex things deserve their own classes Monsters have a name (simple String) but also a Weapon, but that is complex so you need a Weapon class What methods to write? these are the RESPONSIBILITIES of objects of that class - What they DO – Monsters wail for instance It’s the class diagrams that are the hardest part! Once the diagram is done the code is easy