From af3f177f4c923ea5c2730bf906c2bdd6f7304e6c Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Wed, 17 Jun 2020 16:37:41 -0400 Subject: [PATCH] ch11 revisions --- ch11/Time.java | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/ch11/Time.java b/ch11/Time.java index 951d5bf..fe5bfd6 100644 --- a/ch11/Time.java +++ b/ch11/Time.java @@ -25,6 +25,30 @@ public Time(int hour, int minute, double second) { this.second = second; } + public int getHour() { + return this.hour; + } + + public int getMinute() { + return this.minute; + } + + public double getSecond() { + return this.second; + } + + public void setHour(int hour) { + this.hour = hour; + } + + public void setMinute(int minute) { + this.minute = minute; + } + + public void setSecond(double second) { + this.second = second; + } + /** * Prints the time in a simple format. */ @@ -48,9 +72,10 @@ public String toString() { * Tests whether two times are equivalent. */ public boolean equals(Time that) { + final double DELTA = 0.001; return this.hour == that.hour && this.minute == that.minute - && this.second == that.second; + && Math.abs(this.second - that.second) < DELTA; } /** @@ -72,6 +97,7 @@ public Time add(Time t2) { sum.hour = this.hour + t2.hour; sum.minute = this.minute + t2.minute; sum.second = this.second + t2.second; + if (sum.second >= 60.0) { sum.second -= 60.0; sum.minute += 1; @@ -80,22 +106,10 @@ public Time add(Time t2) { sum.minute -= 60; sum.hour += 1; } - return sum; - } - - /** - * Adds the given number of seconds to this object (modifier). - */ - public void increment(double seconds) { - this.second += seconds; - while (this.second >= 60.0) { - this.second -= 60.0; - this.minute += 1; - } - while (this.minute >= 60) { - this.minute -= 60; - this.hour += 1; + if (sum.hour >= 24) { + sum.hour -= 24; } + return sum; } }