1.reading from keyboard 2.main programs 3.responsibilities 1 cs12230 introduction to programming...
DESCRIPTION
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 3TRANSCRIPT
![Page 1: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/1.jpg)
1. Reading from Keyboard2. Main programs 3. Responsibilities 1
CS12230Introduction to Programming
Lecture 2or3-Other things
![Page 2: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/2.jpg)
1. Reading from the keyboard
There are a few ways – we will use ScannerEssentials 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
![Page 3: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/3.jpg)
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
![Page 4: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/4.jpg)
![Page 5: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/5.jpg)
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
![Page 6: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/6.jpg)
Suppose we have a Clock with 2 Times
alarm
time
myClock
6
hrs 8mins 30
hrs 10mins 21
![Page 7: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/7.jpg)
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()
7
2..2
![Page 8: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/8.jpg)
8
![Page 9: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/9.jpg)
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"; } }}
![Page 10: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/10.jpg)
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
![Page 11: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/11.jpg)
• 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(); }
![Page 12: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/12.jpg)
12
![Page 13: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/13.jpg)
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
![Page 14: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/14.jpg)
Note – shows up under classstatic things belong to the class
![Page 15: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/15.jpg)
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
![Page 16: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/16.jpg)
![Page 17: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/17.jpg)
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
![Page 18: 1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things](https://reader035.vdocument.in/reader035/viewer/2022062905/5a4d1ad37f8b9ab0599723a4/html5/thumbnails/18.jpg)
How do you know?What instance variables to put in?
think about what each object of that class HASevery clock has 2 times and is either set or not
Complex things deserve their own classesMonsters 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