Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The ClockDisplay class implements a digital clock display for a
- * European-style 24 hour clock. The clock shows hours and minutes. The
- * range of the clock is 00:00 (midnight) to 23:59 (one minute before
- * midnight).
- *
- * The clock display receives "ticks" (via the timeTick method) every minute
- * and reacts by incrementing the display. This is done in the usual clock
- * fashion: the hour increments when the minutes roll over to zero.
- *
- * @author Michael Kölling and David J. Barnes
- * @version 2016.02.29
- */
- public class ClockDisplay12
- {
- public static final String AM = "a.m.";
- public static final String PM = "p.m.";
- private NumberDisplay hours;
- private NumberDisplay minutes;
- private String amOrPm = AM;
- private String displayString; // simulates the actual display
- /**
- * Constructor for ClockDisplay objects. This constructor
- * creates a new clock set at 00:00.
- */
- public ClockDisplay12()
- {
- hours = new NumberDisplay(12);
- minutes = new NumberDisplay(60);
- updateDisplay();
- }
- /**
- * Constructor for ClockDisplay objects. This constructor
- * creates a new clock set at the time specified by the
- * parameters.
- */
- public ClockDisplay12(int hour, int minute, String amPm)
- {
- hours = new NumberDisplay(12);
- minutes = new NumberDisplay(60);
- setTime(hour, minute, amPm);
- }
- /**
- * This method should get called once every minute - it makes
- * the clock display go one minute forward.
- */
- public void timeTick()
- {
- minutes.increment();
- if(minutes.getValue() == 0) { // it just rolled over!
- if (hours.getValue() == 0){
- if (amOrPm.equals(AM)){
- amOrPm = PM;
- } else {
- amOrPm = AM;
- }
- }
- hours.increment();
- }
- updateDisplay();
- }
- /**
- * Set the time of the display to the specified hour and
- * minute.
- */
- public void setTime(int hour, int minute, String amPm)
- {
- if (hour == 12)
- {
- hour = 0;
- }
- hours.setValue(hour);
- minutes.setValue(minute);
- if (amPm.equals(ClockDisplay12.AM) || amPm.equals(ClockDisplay12.PM))
- {
- amOrPm = amPm;
- updateDisplay();
- return;
- }
- amOrPm = ClockDisplay12.AM;
- updateDisplay();
- return;
- }
- /**
- * Return the current time of this display in the format HH:MM.
- */
- public String getTime()
- {
- return displayString;
- }
- /**
- * Update the internal string that represents the display.
- */
- private void updateDisplay()
- {
- if (hours.getDisplayValue().equals("00")){
- displayString = "12" + ":" +
- minutes.getDisplayValue() + amOrPm;
- return;
- }
- displayString = hours.getValue() + ":" +
- minutes.getDisplayValue() + amOrPm;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement