Java Reference
In-Depth Information
Code 3.4
continued
Implementation of the
ClockDisplay
class
public void timeTick()
{
minutes.increment();
if (minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
}
/**
* Set the time of the display to the specified hour and
* minute.
*/
public void setTime( int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}
/**
* Return the current time of this display in the format
* HH:MM.
*/
public String getTime()
{
return displayString;
}
/**
* Update the internal string that represents the
* display.
*/
private void updateDisplay()
{
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
}
In this project, we use the field displayString to simulate the actual display device of the
clock (as you could see in Exercise 3.5). Were this software to run in a real clock, we would
present the output on the real clock display instead. So this string serves as our software simula-
tion for the clock's output device. 1
1 The topic projects folder also includes a version of this project with a simple graphical user interface
(GUI), named clock-display-with-GUI . The curious reader may like to experiment with this project;
however, it will not be discussed in this topic.
 
Search WWH ::




Custom Search