import java.io.*; class Time { // Time // Constructor for the Time class // Parameters: int hours, int minutes, String amOrPm // Pre: none // Post: an object of type Time will be created with the parameters // as given // Returns: reference to a new Time object // Called by: ? // Calls: none public Time(int hours, int minutes, String amOrPm) { if (amOrPm.equals ("am")) { if (hours == 12) // check to see if it is midnight hours = 0; // if so, set hours to military hours ex: (0:00) } else // pm if (hours != 12) // if after noon, add 12 hours to make military time ex: 14:00 hours += 12 totalMinutes= hours * 60 + minutes; // convert hours and minutes into minutes } // Time // Used by addDuration // Parameters: int minutes // Pre: none // Post: updates the total minutes // Returns: none // Called by: addDuration // Calls: none private Time(int minutes) { this.totalMinutes = minutes; } // addDuration // adds time to the currently stored time // Parameters: int minutes // Pre: none // Post: the input minutes will be added to the currently stored minutes and a new object returned // as given // Returns: reference to a new Time object // Called by: ? // Calls: Time public Time addDuration (int minutes){ int newTotalMinutes = totalMinutes + minutes; Time newTime = new Time(newTotalMinutes); return newTime; } // addDuration // adds time to the currently stored time // Parameters: int hours, int minutes // Pre: none // Post: the input hours and minutes will be added to the currently stored minutes and a new object returned // as given // Returns: reference to a new Time object // Called by: ? // Calls: Time public Time addDuration (int hours, int minutes){ int newTotalMinutes = totalMinutes + (hours *60 + minutes); Time newTime = new Time(newTotalMinutes); return newTime; } // isBefore // compares two times and determines if the time stored in this object is before // the input time // Parameters: Time timeToCompare // Pre: another Time object must exist // Post: the two times will be compared and if the current Time object stores a time less than // the input time, a true will be returned // Returns: boolean // Called by: ? // Calls: none public boolean isBefore (Time t){ return totalMinutes < t.totalMinutes; } // isAfter // compares two times and determines if the time stored in this object is after // the input time // Parameters: Time timeToCompare // Pre: another Time object must exist // Post: the two times will be compared and if the current Time object stores a time greater than // the input time, a true will be returned // Returns: boolean // Called by: ? // Calls: none public boolean isAfter(Time t){ return totalMinutes < t.totalMinutes; } // print - prints all the information for the given Time // Parameters: none // Pre: an Time object must exist // Post: none // Returns: none // Called by: ? // Calls: none // public void print (){ int hours = totalMinutes / 100; // convert total minutes to hours boolean isAfternoon; if (hours >= 12){ // if after noon convert back to standard time (vs. military) isAfternoon=true; hours = hours - 12; } else { isAfternoon = false; if (hours = 0) hours = 12; } // end else System.out.print(hours); System.out.print(":"); System.out.print(totalMinutes%60); // convert total minutes to minutes if(isAfternoon); System.out.print(" pm"); else System.out.print(" am"); System.out.println(); } // end print private int totalMinutes; } // end Time