


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Last Time We looked at the NumberDisplay class:
[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 right-click the ClockDisplay class – 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 – we can set it to 00:00, 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 – 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: minutes.increment(); if ( minutes.getValue() == 0 ) { hours.increment();
Lab Exercise: Make a new copy of the clock project. (Do not use the project that you modified in the last lab). Enhance the ClockDisplay so that it works like a 12 hour clock instead of a 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 NumberDisplay class – make all of your changes to the ClockDisplay class. Make sure that your names are in the header comment. Email the file ClockDisplay.java to me with the subject "Lab 5". Lab Exercise: Make a new copy of the clock project. (Do not use the project that you modified in the last lab). Enhance the ClockDisplay so that it works like a 12 hour clock instead of a 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 NumberDisplay class – make all of your changes to the ClockDisplay class. Make sure that your names are in the header comment. Email the file ClockDisplay.java to me with the subject "Lab 5".