-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time.java
66 lines (55 loc) · 1.25 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
import java.io.Serializable;
//Create the time class
public class Time implements Serializable {
private static final long serialVersionUID = 10;
private int hours;
private int minutes;
public Time () {
hours = 0;
minutes = 0;
}
public Time (int h, int m) {
set (h, m);
}
public int getHours () {
return hours;
}
public int getMinutes () {
return minutes;
}
public void set (int h, int m) {
hours = h;
minutes = m;
}
public Time clone () {
Time t = new Time (hours, minutes);
return t;
}
public boolean equals (Time l) {
if (hours == l.getHours() && minutes == l.getMinutes()) {
return true;
}
else {
return false;
}
}
public int compareTo (Time l) {
int totalMinutes = this.getHours() * 60 + this.getMinutes();
int totalMinutes2 = l.getHours() * 60 + l.getMinutes();
return totalMinutes - totalMinutes2;
}
public String toString () {
return (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes;
}
public Time advanceMinutes (int minutes) {
int totalMinutes = hours * 60 + this.minutes;
totalMinutes += minutes;
hours = totalMinutes / 60;
if (hours > 24) {
hours -= 24;
}
this.minutes = totalMinutes % 60;
Time t = new Time (hours, this.minutes);
return t;
}
}