Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. /**
  2.  * The ClockDisplay class implements a digital clock display for a
  3.  * European-style 24 hour clock. The clock shows hours and minutes. The
  4.  * range of the clock is 00:00 (midnight) to 23:59 (one minute before
  5.  * midnight).
  6.  *
  7.  * The clock display receives "ticks" (via the timeTick method) every minute
  8.  * and reacts by incrementing the display. This is done in the usual clock
  9.  * fashion: the hour increments when the minutes roll over to zero.
  10.  *
  11.  * Exercise 3.38 & 3.39
  12.  * Create a 12 hour clock instead.
  13.  *
  14.  * @author Michael Kölling and David J. Barnes
  15.  * @version 2016.02.29
  16.  */
  17. public class ClockDisplay
  18. {
  19.     private NumberDisplay hours;
  20.     private NumberDisplay minutes;
  21.     private String displayString;    // simulates the actual display
  22.    
  23.     /**
  24.      * Constructor for ClockDisplay objects. This constructor
  25.      * creates a new clock set at 00:00.
  26.      */
  27.     public ClockDisplay()
  28.     {
  29.         hours = new NumberDisplay(24);
  30.         minutes = new NumberDisplay(60);
  31.         updateDisplay();
  32.     }
  33.  
  34.     /**
  35.      * Constructor for ClockDisplay objects. This constructor
  36.      * creates a new clock set at the time specified by the
  37.      * parameters.
  38.      */
  39.     public ClockDisplay(int hour, int minute)
  40.     {
  41.         hours = new NumberDisplay(24);
  42.         minutes = new NumberDisplay(60);
  43.         setTime(hour, minute);
  44.     }
  45.  
  46.     /**
  47.      * This method should get called once every minute - it makes
  48.      * the clock display go one minute forward.
  49.      */
  50.     public void timeTick()
  51.     {
  52.         minutes.increment();
  53.         if(minutes.getValue() == 0) {  // it just rolled over!
  54.             hours.increment();
  55.         }
  56.         updateDisplay();
  57.     }
  58.  
  59.     /**
  60.      * Set the time of the display to the specified hour and
  61.      * minute.
  62.      */
  63.     public void setTime(int hour, int minute)
  64.     {
  65.         hours.setValue(hour);
  66.         minutes.setValue(minute);
  67.         updateDisplay();
  68.     }
  69.  
  70.     /**
  71.      * Return the current time of this display in the format HH:MM.
  72.      */
  73.     public String getTime()
  74.     {
  75.         return displayString;
  76.     }
  77.    
  78.     /**
  79.      * Update the internal string that represents the display.
  80.      */
  81.     private void updateDisplay()
  82.     {
  83.         int hour = hours.getValue();
  84.         String suffix = "am";
  85.         if (hour >= 12) {
  86.             hour = hour - 12;
  87.             suffix = "pm";
  88.         }
  89.         if(hour == 0) {
  90.             hour = 12;
  91.         }
  92.         displayString = hour + "." + minutes.getDisplayValue() + suffix;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement