-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock
More file actions
59 lines (49 loc) · 1.1 KB
/
Clock
File metadata and controls
59 lines (49 loc) · 1.1 KB
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
public class Main {
public static void main(String[] args) {
java.util.Scanner in = new java.util.Scanner(System.in);
Clock clock = new Clock(in.nextInt(), in.nextInt(), in.nextInt());
clock.tick();
System.out.println(clock);
in.close();
}
}
class Clock {
private Display hour = new Display(24);
private Display minute = new Display(60);
private Display second = new Display(60);
public Clock(int hour, int minute, int second) {
this.hour.startTime(hour);
this.minute.startTime(minute);
this.second.startTime(second);
}
public void tick() {
if (second.increase())
if (minute.increase()) {
hour.increase();
}
}
public String toString() {
return String.format("%02d:%02d:%02d", hour.getValue(), minute.getValue(), second.getValue());
}
}
class Display {
private int value = 0;
private int limit = 0;
public Display(int limit) {
this.limit = limit;
}
public void startTime(int value) {
this.value = value;
}
public boolean increase() {
value++;
if (value == limit) {
value = 0;
return true;
}
return false;
}
public int getValue() {
return value;
}
}