Advertisement
Guest User

ClockDisplay12

a guest
Sep 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 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. * @author Michael Kölling and David J. Barnes
  12. * @version 2016.02.29
  13. */
  14. public class ClockDisplay12
  15. {
  16. public static final String AM = "a.m.";
  17. public static final String PM = "p.m.";
  18. private NumberDisplay hours;
  19. private NumberDisplay minutes;
  20. private String amOrPm = AM;
  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 ClockDisplay12()
  28. {
  29. hours = new NumberDisplay(12);
  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 ClockDisplay12(int hour, int minute, String amPm)
  40. {
  41. hours = new NumberDisplay(12);
  42. minutes = new NumberDisplay(60);
  43. setTime(hour, minute, amPm);
  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. if (hours.getValue() == 0){
  55. if (amOrPm.equals(AM)){
  56. amOrPm = PM;
  57. } else {
  58. amOrPm = AM;
  59. }
  60. }
  61. hours.increment();
  62. }
  63. updateDisplay();
  64. }
  65.  
  66.  
  67. /**
  68. * Set the time of the display to the specified hour and
  69. * minute.
  70. */
  71. public void setTime(int hour, int minute, String amPm)
  72. {
  73. if (hour == 12)
  74. {
  75. hour = 0;
  76. }
  77. hours.setValue(hour);
  78. minutes.setValue(minute);
  79. if (amPm.equals(ClockDisplay12.AM) || amPm.equals(ClockDisplay12.PM))
  80. {
  81. amOrPm = amPm;
  82. updateDisplay();
  83. return;
  84. }
  85. amOrPm = ClockDisplay12.AM;
  86. updateDisplay();
  87. return;
  88. }
  89.  
  90. /**
  91. * Return the current time of this display in the format HH:MM.
  92. */
  93. public String getTime()
  94. {
  95. return displayString;
  96. }
  97.  
  98. /**
  99. * Update the internal string that represents the display.
  100. */
  101. private void updateDisplay()
  102. {
  103. if (hours.getDisplayValue().equals("00")){
  104. displayString = "12" + ":" +
  105. minutes.getDisplayValue() + amOrPm;
  106. return;
  107. }
  108. displayString = hours.getValue() + ":" +
  109. minutes.getDisplayValue() + amOrPm;
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement