2-4-or-3-1-other

Download Report

Transcript 2-4-or-3-1-other

CS12230
Introduction to Programming
Lecture 2or3-Other things
1. Reading from Keyboard
2. Main programs
3. Responsibilities
1
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:
http://docs.oracle.com/javase/7/docs/api/
• 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
myClock
alarm
time
hrs 8
mins 30
hrs 10
mins 21
6
Here is the Class diagram (code in 2-week2-clock)
code on next slide – just look at column 1 first
Time
---------------------int hrs
-int mins
--------------------+Time ()
+void setTime(int h, int m)
+String toString()
2..2
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()
7
8
public class Clock{ //horrible formatting to fit
private Time alarm; private Time time;
boolean isSet;
public class Time{
private int hrs;
private int mins;
public Clock(){
alarm=new Time(); time=new Time();
isSet=false;}
public Time() {
hrs=1;
mins=0;
}
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 void setTime(int h, int m) {
hrs=h;
mins=m;
}
public String toString() {
return hrs+" : "+mins;
}
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 adhoc
• 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