GSculerlor

ClockDisplay.java

Oct 5th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package ObjectInteraction;
  2.  
  3. public class ClockDisplay
  4. {
  5.     private NumberDisplay hours;
  6.     private NumberDisplay minutes;
  7.     private String displayString; // simulates the actual display
  8.  
  9.     /**
  10.      * Constructor for ClockDisplay objects. This constructor
  11.      * creates a new clock set at 00:00.
  12.      */
  13.     public ClockDisplay() {
  14.         hours = new NumberDisplay(24);
  15.         minutes = new NumberDisplay(60);
  16.         updateDisplay();
  17.     }
  18.    
  19.     /**
  20.      * Constructor for ClockDisplay objects. This constructor
  21.      * creates a new clock set at the time specified by the
  22.      * parameters.
  23.      */
  24.     public ClockDisplay(int hour, int minute) {
  25.         hours = new NumberDisplay(24);
  26.         minutes = new NumberDisplay(60);
  27.         setTime(hour, minute);
  28.     }
  29.    
  30.     /**
  31.      * This method should get called once every minute - it
  32.      * makes the clock display go one minute forward.
  33.      */
  34.     public void timeTick() {
  35.         minutes.increment();
  36.         if(minutes.getValue() == 0) { // it just rolled over!
  37.             hours.increment();
  38.         }
  39.         updateDisplay();
  40.     }
  41.    
  42.     /**
  43.      * Set the time of the display to the specified hour and
  44.      * minute.
  45.      */
  46.     public void setTime(int hour, int minute) {
  47.         hours.setValue(hour);
  48.         minutes.setValue(minute);
  49.         updateDisplay();
  50.     }
  51.    
  52.     /**
  53.      * Return the current time of this display in the format
  54.      * HH:MM.
  55.      */
  56.     public String getTime() {
  57.         return displayString;
  58.     }
  59.    
  60.     /**
  61.      * Update the internal string that represents the
  62.      * display.
  63.      */
  64.     private void updateDisplay() {
  65.         int hour = hours.getValue();
  66.         String suffix;
  67.        
  68.         if (hour >= 12) {
  69.             suffix = " pm";
  70.             if (hour > 12) hours.setValue(hour-12);
  71.         } else {
  72.             suffix = " am";
  73.         }
  74.        
  75.         displayString = hours.getDisplayValue() + ":" +
  76.             minutes.getDisplayValue() + suffix;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment