-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time.java
78 lines (67 loc) · 1.47 KB
/
Time.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
//Ashley Zhang + Grace Gent
public class Time implements Comparable<Time>
{
private int hour;
private int minute;
public Time(int h, int m) {
hour = h;
minute = m;
}
public void setHour(int hr) {
hour = hr;
}
public int getHour() {
return hour;
}
public void setMinute(int min) {
minute = min;
}
public int getMinute() {
return minute;
}
public void advance(int min) {
if(minute + min < 60) {
minute += min;
}
else {
hour += (minute + min) / 60;
minute = (minute + min) % 60;
}
}
public int minBetween(Time other) {
if(hour == other.getHour()) {
return Math.abs(other.getMinute() - minute);
}
else if(hour < other.getHour()) {
return (other.getHour() - hour) * 60 + other.getMinute() - minute;
}
else {
return(hour - other.getHour()) * 60 + minute - other.getMinute();
}
}
public String toString() {
if(minute < 10) {
return hour + ":0" + minute;
}
else {
return hour + ":" + minute;
}
}
public int compareTo(Time other) {
if(hour < other.getHour()) {
return -1;
}
else if(hour > other.getHour()) {
return 1;
}
else {
if(minute < other.getMinute()) {
return -1;
}
else if(minute > other.getMinute()) {
return 1;
}
}
return 0;
}
}