This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathClockDisplay.java
89 lines (82 loc) · 2.53 KB
/
ClockDisplay.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* The ClockDisplay class implements a digital clock display for a
* European-style 24 hour clock. The clock shows hours and minutes. The
* range of the clock is 00:00 (midnight) to 23:59 (one minute before
* midnight).
*
* The clock display receives "ticks" (via the timeTick method) every minute
* and reacts by incrementing the display. This is done in the usual clock
* fashion: the hour increments when the minutes roll over to zero.
*
* @author Michael Kolling and David J. Barnes
* @version 2008.03.30
*/
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private NumberDisplay seconds;
private String displayString; // simulates the actual display
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
seconds = new NumberDisplay(60);
updateDisplay();
}
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at the time specified by the
* parameters.
*/
public ClockDisplay(int hour, int minute, int second)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute, second);
}
/**
* This method should get called once every minute - it makes
* the clock display go one minute forward.
*/
public void timeTick()
{
seconds.increment();
if(seconds.getValue() == 0) { // it just rolled over!
minutes.increment();
if(minutes.getValue()==0){
hours.increment();
}
}
updateDisplay();
}
/**
* Set the time of the display to the specified hour and
* minute.
*/
public void setTime(int hour, int minute, int second)
{
hours.setValue(hour);
minutes.setValue(minute);
seconds.setValue(second);
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() + " : " + seconds.getDisplayValue();
}
}