-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyTime.java
66 lines (53 loc) · 1.22 KB
/
MyTime.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
public class MyTime implements Cloneable {
private int value;
public MyTime(int hour, int min, int sec) {
if (hour < 0 || min < 0 || min >= 60 || sec < 0 || sec >= 60)
throw new IllegalArgumentException("illegal time "+hour+":"+min+":"+sec);
this.value = hour * 3600 + min * 60 + sec;
}
public MyTime(int value) {
if (value < 0)
throw new IllegalArgumentException("illegal time "+value+" seconds");
this.value = value;
}
public int getHour() {
return value / 3600;
}
public int getMin() {
return (value / 60) % 60;
}
public int getSec() {
return value % 60;
}
public int getValue()
{
return value;
}
public void increment() {
value++;
}
public MyTime add(MyTime other) {
value += other.value;
return this;
}
public boolean equals(Object obj) {
if (obj instanceof MyTime) {
MyTime other = (MyTime)obj;
return value==other.value;
}
else
return false;
}
public int compareTo(MyTime t) {
return value - t.value;
}
public int hashCode() {
return value;
}
public String toString() {
return getHour()+":"+getMin()+":"+getSec();
}
public Object clone() {
return new MyTime(value);
}
}