Intro to Programming in Java
Intro to Programming in Java CSIS 110
Popular in Course
Popular in Computer Information Systems
This 4 page Class Notes was uploaded by Laurianne White MD on Monday October 12, 2015. The Class Notes belongs to CSIS 110 at Fort Lewis College taught by Brian Hanks in Fall. Since its upload, it has received 27 views. For similar materials see /class/221848/csis-110-fort-lewis-college in Computer Information Systems at Fort Lewis College.
Reviews for Intro to Programming in Java
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 10/12/15
CSIS 110 Lecture 8 Last Time We looked at the NumberDisplay class Abstraction and modularization boolean operations amp amp l l and Modulo operator ClockDisplay Now create a ClockDisplay object and play around with it a little bit Let s look at the elds for this class private NumberDisplay hours private NumberDisplay minutes private String displayString Important point 7 two ofthese elds have the type NumberDi spl ay which is the class we just looked at So where do these two objects come from Look at the rst constructor the one with no arguments hours new NumberDisplay24 minutes new NumberDisplay60 These statements invoke the NumberDisplay constructor just like you did by hand from Blue when you select that choice from the class menu However you won39t see them displayed in BlueJ So when we create a new ClockDisplay its constructor automatically creates the two NumberDisplay objects for us It does this using the new operator which is used in statements this way new ClassName parameter list The new operation does 2 things 1 It creates a new object of the named class for example NumberDisplay 2 It invokes that class s constructor If the constructor is de ned with formal parameters then the new operation needs to specify values for the actual parameters Draw a picture of what this looks like KEY POINT An object can create other objects We saw this before with the picture project Multiple Constructors Let s look at the menu when we rightclick the ClockDisplay class 7 there are two ways to create a new ClockDisplay What s going on The ClockDisplay class contains two constructors How do they differ This allows us to initialize a ClockDisplay in different ways 7 we can set it to 0000 or we can set it to any time that we want What happens if we try to create a ClockDisplay with invalid time This is called overloading the constructor Internal Method Calls There are some statements in the constructors that look a little different updateDisplay setTime hour minute These are method calls If we look at the ClockDisplay class we see that it includes these methods public void updateDisplay public void setTime int hour int minute The above statements call or invoke the method 7 because the methods are defined in the same class as the call statement we call these internal method calls So when we see this statement we call that method for the current object When a method is called control is transferred to the method The statements in the method are executed When the method is done control is transferred back to the calling statement and then continues with the statement after that External Method Calls Let s look at the timeTick method It includes statements such as minutesincrement if minutesgetValue 0 hoursincrement What is the statement minutes increment It is also a method call but to a method associated with a dz erent object In this case it invokes the increment method on the minutes object which is an instance of NumberDisplay A method call to a method of a different object is called an external method call External method calls have the form objectmethodName parameter list Important 7 we use the name of the object here not the name of its class Lab Exercise Make a new copy of the clock project Do not use the project that you modi ed in the last lab Enhance the ClockDi spl ay so that it works like a 12 hour clock instead ofa 24 hour clock That is the hours should count from 1 to 12 and the minutes should count from 0 to 59 Do NOT modify the NumberDi spl ay class 7 make all of your changes to the ClockDisplay class Make sure that your names are in the header comment Email the le C10ckDisp1ayj ava to me with the subject quotLab 5quot Lab Exercise Make a new copy of the clock project Do not use the project that you modi ed in the last lab Enhance the ClockDi spl ay so that it works like a 12 hour clock instead ofa 24 hour clock That is the hours should count from 1 to 12 and the minutes should count from 0 to 59 Do NOT modify the NumberDi spl ay class 7 make all of your changes to the ClockDisplay class Make sure that your names are in the header comment Email the le C10ckDisp1ayj ava to me with the subject quotLab 5quot