/** * This version of ClockDisplay implements everything in the * ClockDisplay class, rather than building off of NumberDisplay. * * @author Michael Kölling, David J. Barnes, and Brad Richards * @version 2.0 */ public class ClockDisplay_merged { private int hoursValue; private int minutesValue; private String displayString; // simulates the actual display public final int MIN_LIMIT = 60; public final int HOUR_LIMIT = 24; /** * Constructor for ClockDisplay objects. This constructor * creates a new clock set at 00:00. */ public ClockDisplay_merged() { hoursValue = 0; minutesValue = 0; updateDisplay(); } /** * Constructor for ClockDisplay objects. This constructor * creates a new clock set at the time specified by the * parameters. * @param hour The hour to use in setting time * @param minute The minute value to use in setting time */ public ClockDisplay_merged(int hour, int minute) { setTime(hour, minute); } /** * This method should get called once every minute - it makes * the clock display go one minute forward. */ public void timeTick() { minutesValue = minutesValue + 1; if (minutesValue > MIN_LIMIT) { minutesValue = 0; hoursValue = hoursValue + 1; if (hoursValue > HOUR_LIMIT) { hoursValue = 0; } } updateDisplay(); } /** * Set the time of the display to the specified hour and * minute. * @param hour The hour to use in setting time * @param minute The minute value to use in setting time */ public void setTime(int hour, int minute) { hoursValue = hour; minutesValue = minute; updateDisplay(); } /** * Return the current time of this display in the format HH:MM. * @return Returns the time as a string. */ public String getTime() { return displayString; } /** * Update the internal string that represents the display. */ private void updateDisplay() { String hoursString; String minutesString; if (hoursValue < 10) { hoursString = "0" + hoursValue; } else { hoursString = "" + hoursValue; } if (minutesValue < 10) { minutesString = "0" + minutesValue; } else { minutesString = "" + minutesValue; } displayString = hoursString + ":" + minutesString; } }