// Represents a time span of hours and minutes elapsed. // Class invariant: minutes < 60 public class TimeSpan { private int minutes; // Constructs a time span with the given interval // Precondition: hours >= 0 and minutes >= 0 public TimeSpan () { minutes = 0; } // Constructs a time span with the given interval // Precondition: hours >= 0 and minutes >= 0 public TimeSpan (int hrs, int min) { minutes = 0; add(hrs, min); } //------------------------------- // Sets the time span to the given interval // Precondition: hours >= 0 and minutes >= 0 public void set(int hrs, int min) { minutes = 0; add(hrs, min); } //------------------------------- // Resets the time span to zero public void reset() { minutes = 0; } //---------------------------------- // Add the given interval to the time span // Precondition: hours >= 0 and minutes >= 0 public void add(int hrs, int min) { minutes += hrs * 60 + min; } //----------------------------------- // Returns whether obj is a TimeSpan representing the same // number of hours and minutes as this TimeSpan object public boolean equals(Object obj) { if (obj instanceof TimeSpan) { TimeSpan candidate = (TimeSpan) obj; return (minutes == candidate.minutes); } else { return false; } } //--------------------------------------- // Returns a String for this time span, e.g. "6h15m" public String toString() { int hours = minutes / 60; return hours + "h" + (minutes % 60) + "m"; } }