-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.java
More file actions
101 lines (76 loc) · 1.57 KB
/
Clock.java
File metadata and controls
101 lines (76 loc) · 1.57 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
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
90
91
92
93
94
95
96
97
98
99
100
101
public class Clock {
private int hour; // 0-23 hours
private int minute; // 0-59 minutes
private double second; // 0-59 seconds
private double grain; // 0-3600 seconds
public Clock (int h, int m, double s, double g)
{
if (h < 0 || h > 23)
minute = ( (m >= 0 && h < 59) ? m : 0);
second = ( (s >= 0.0 && h < 59) ? s : 0.0);
grain = 0.0;
}
public void setHour(int h)
{
hour = h;
}
public int getHour()
{
return hour;
}
public void setMinute(int m)
{
minute = m;
}
public int getMinute()
{
return minute;
}
public void setSecond(double s)
{
second = s;
}
public double getSecond()
{
return second;
}
public void setGrain(double g)
{
grain = g;
}
public double getGrain()
{
return grain;
}
public void tick () // how the clock works
{
second += grain;
if (second >= 60.0) // Method for outputting seconds
{
minute += (int)(second/60);
second %= 60;
}
if (minute > 59) // Method for outputting minutes
{
hour += minute/60;
minute %= 60;
}
if (hour >= 24) // Method for outputting hours
{
hour = 0;
}
}
@Override
public String toString() //Output time
{
return String.format("%02d:%02d:%02d", hour, minute, second);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Clock c = new Clock(2,42,38,400);
System.out.printf("The time is:", c.getHour(), c.getMinute(), c.getSecond(), c.getGrain());
}
}