/** * We'll write our own version of the book's NumberDisplay class for * practice. A NumberDisplay JUST keeps track of a two-digit number. * We'll eventually use two of these in the clock -- one for hours * and one for minutes. * * @author Brad Richards * @version 1.0 */ public class NumberDisplay { private int value = 0; // Always start this out at zero private int upperLimit; // Number you can "never reach" -- 60 for minutes /** * Constructor that creates a NumberDisplay with the specified upper limit. * * @param rollOverLimit The value at which the display should roll over. */ public NumberDisplay(int rollOverLimit) { upperLimit = rollOverLimit; } /** * Increment increases the value of the clock by one, wrapping around if * necessary. */ public void increment() { value = value + 1; // Increment the value if (value >= upperLimit) { // Fix it if it got too large value = 0; } } /** * Returns a string version of the displayed value. If it's a single digit, we * stick an extra zero in front of it. * * @return Returns current value as a two-character string. */ public String getDisplayValue() { if (value < 10) { return "0"+value; } else { return ""+value; } } /** * A "getter" that returns the current display value. * * @return Current value of NumberDisplay. */ public int getValue() { return value; // For now... } /** * Sets the display to the specified value, as long as the specified value is * within the appropriate range. Otherwise we print an error message and leave * the value unchanged. * * @param newValue A new value that's >= 0 and < roll over limit. */ public void setValue(int newValue) { if (newValue >= 0 && newValue < upperLimit) { value = newValue; } else { System.out.println("Proposed value is out of range! Ignoring request."); } } }