Skip to content

Commit af3f177

Browse files
committed
ch11 revisions
1 parent fbd0dca commit af3f177

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

ch11/Time.java

+30-16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ public Time(int hour, int minute, double second) {
2525
this.second = second;
2626
}
2727

28+
public int getHour() {
29+
return this.hour;
30+
}
31+
32+
public int getMinute() {
33+
return this.minute;
34+
}
35+
36+
public double getSecond() {
37+
return this.second;
38+
}
39+
40+
public void setHour(int hour) {
41+
this.hour = hour;
42+
}
43+
44+
public void setMinute(int minute) {
45+
this.minute = minute;
46+
}
47+
48+
public void setSecond(double second) {
49+
this.second = second;
50+
}
51+
2852
/**
2953
* Prints the time in a simple format.
3054
*/
@@ -48,9 +72,10 @@ public String toString() {
4872
* Tests whether two times are equivalent.
4973
*/
5074
public boolean equals(Time that) {
75+
final double DELTA = 0.001;
5176
return this.hour == that.hour
5277
&& this.minute == that.minute
53-
&& this.second == that.second;
78+
&& Math.abs(this.second - that.second) < DELTA;
5479
}
5580

5681
/**
@@ -72,6 +97,7 @@ public Time add(Time t2) {
7297
sum.hour = this.hour + t2.hour;
7398
sum.minute = this.minute + t2.minute;
7499
sum.second = this.second + t2.second;
100+
75101
if (sum.second >= 60.0) {
76102
sum.second -= 60.0;
77103
sum.minute += 1;
@@ -80,22 +106,10 @@ public Time add(Time t2) {
80106
sum.minute -= 60;
81107
sum.hour += 1;
82108
}
83-
return sum;
84-
}
85-
86-
/**
87-
* Adds the given number of seconds to this object (modifier).
88-
*/
89-
public void increment(double seconds) {
90-
this.second += seconds;
91-
while (this.second >= 60.0) {
92-
this.second -= 60.0;
93-
this.minute += 1;
94-
}
95-
while (this.minute >= 60) {
96-
this.minute -= 60;
97-
this.hour += 1;
109+
if (sum.hour >= 24) {
110+
sum.hour -= 24;
98111
}
112+
return sum;
99113
}
100114

101115
}

0 commit comments

Comments
 (0)