Wout12345

Object-oriented programming in Java - Session 1

Feb 13th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. public class DigitalClock {
  2.  
  3. /**
  4. * ...
  5. *
  6. * @post ...
  7. * | new.getHours() == 0
  8. * ...
  9. * | new.getMinHours() == 0
  10. * ...
  11. * | new.getMaxHours() == 23
  12. * ...
  13. * | new.getMinutes() == 0
  14. */
  15. public DigitalClock() {
  16.  
  17. }
  18.  
  19. /**
  20. * ...
  21. *
  22. * @param hours
  23. * The amount of hours of the new clock.
  24. * @param minutes
  25. * The amount of minutes of the new clock.
  26. * @effect ...
  27. * | setHours(hours)
  28. * ...
  29. * | setMinutes(minutes)
  30. * @post ...
  31. * | new.getHours() == hours
  32. * ...
  33. * | new.getMinHours() == 0
  34. * ...
  35. * | new.getMaxHours() == 23
  36. * ...
  37. * | new.getMinutes() == minutes
  38. */
  39. public DigitalClock(int hours, int minutes) {
  40. setHours(hours);
  41. setMinutes(minutes);
  42. }
  43.  
  44. /**
  45. * Returns the amount of hours of this digital clock.
  46. *
  47. * @return
  48. */
  49. public int getHours() {
  50. return this.hours;
  51. }
  52.  
  53. /**
  54. * Returns the amount of minutes of this digital clock.
  55. *
  56. * @return
  57. */
  58. public int getMinutes() {
  59. return this.minutes;
  60. }
  61.  
  62. public int getMinHours() {
  63. return 0;
  64. }
  65.  
  66. public int getMaxHours() {
  67. return 23;
  68. }
  69.  
  70. /**
  71. * Sets the amount of hours of this digital clock.
  72. *
  73. * @param hours
  74. * The new amount of hours of this digital clock.
  75. * @post The new amount of hours of this clock will be equal to the given hours
  76. * if the given hours are within the bounds of this clock.
  77. * | if (hours >= getMinHours() && hours <= getMaxHours())
  78. * | then new.getHours() == hours
  79. * @post ...
  80. * | if (hours < getMinHours())
  81. * | then new.getHours() == getMinHours()
  82. * @post ...
  83. * | if (hours > getMaxHours())
  84. * | then new.getHours() == (hours - getMinHours())%(getMaxHours() - getMinHours() + 1) + getMinHours()
  85. */
  86. public void setHours(int hours) {
  87. if (hours < getMinHours()) {
  88. this.hours = getMinHours();
  89. } else if (hours > getMaxHours()) {
  90. this.hours = (hours - getMinHours())%(getMaxHours() - getMinHours() + 1) + getMinHours();
  91. } else {
  92. this.hours = hours;
  93. }
  94. }
  95.  
  96. /**
  97. * Sets the amount of minutes of this digital clock
  98. *
  99. * @param minutes
  100. * The new amount of minutes of this digital clock.
  101. * @post The new amount of minutes of this clock will be equal to the given minutes
  102. * if the given minutes are larger or equal to 0 and smaller than 60.
  103. * | new.getMinutes() == Math.abs(minutes%60)
  104. */
  105. public void setMinutes(int minutes) {
  106. this.minutes = Math.abs(minutes%60);
  107. }
  108.  
  109. /**
  110. * Increments the time by one minute
  111. *
  112. * @post ...
  113. * | new.getMinutes() = (getMinutes() + 1)%60
  114. */
  115. public void incrementTime() {
  116. if (getMinutes() == 59) {
  117. setHours(getHours() + 1);
  118. }
  119. setMinutes(getMinutes() + 1);
  120. }
  121.  
  122. /**
  123. * Checks whether clock1 is set to an earlier time than clock2.
  124. *
  125. * @return Returns true if clock1 is set to an earlier time than clock2.
  126. * @param clock1
  127. * The first clock.
  128. * @param clock2
  129. * The second clock.
  130. */
  131. public boolean isEarlierThan(DigitalClock clock1, DigitalClock clock2) {
  132. if (clock1.getHours() < clock2.getHours() || clock1.getMinutes() < clock2.getMinutes()) {
  133. return true;
  134. }
  135. return false;
  136. }
  137.  
  138. private int hours;
  139. private int minutes;
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment