Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DigitalClock {
- /**
- * ...
- *
- * @post ...
- * | new.getHours() == 0
- * ...
- * | new.getMinHours() == 0
- * ...
- * | new.getMaxHours() == 23
- * ...
- * | new.getMinutes() == 0
- */
- public DigitalClock() {
- }
- /**
- * ...
- *
- * @param hours
- * The amount of hours of the new clock.
- * @param minutes
- * The amount of minutes of the new clock.
- * @effect ...
- * | setHours(hours)
- * ...
- * | setMinutes(minutes)
- * @post ...
- * | new.getHours() == hours
- * ...
- * | new.getMinHours() == 0
- * ...
- * | new.getMaxHours() == 23
- * ...
- * | new.getMinutes() == minutes
- */
- public DigitalClock(int hours, int minutes) {
- setHours(hours);
- setMinutes(minutes);
- }
- /**
- * Returns the amount of hours of this digital clock.
- *
- * @return
- */
- public int getHours() {
- return this.hours;
- }
- /**
- * Returns the amount of minutes of this digital clock.
- *
- * @return
- */
- public int getMinutes() {
- return this.minutes;
- }
- public int getMinHours() {
- return 0;
- }
- public int getMaxHours() {
- return 23;
- }
- /**
- * Sets the amount of hours of this digital clock.
- *
- * @param hours
- * The new amount of hours of this digital clock.
- * @post The new amount of hours of this clock will be equal to the given hours
- * if the given hours are within the bounds of this clock.
- * | if (hours >= getMinHours() && hours <= getMaxHours())
- * | then new.getHours() == hours
- * @post ...
- * | if (hours < getMinHours())
- * | then new.getHours() == getMinHours()
- * @post ...
- * | if (hours > getMaxHours())
- * | then new.getHours() == (hours - getMinHours())%(getMaxHours() - getMinHours() + 1) + getMinHours()
- */
- public void setHours(int hours) {
- if (hours < getMinHours()) {
- this.hours = getMinHours();
- } else if (hours > getMaxHours()) {
- this.hours = (hours - getMinHours())%(getMaxHours() - getMinHours() + 1) + getMinHours();
- } else {
- this.hours = hours;
- }
- }
- /**
- * Sets the amount of minutes of this digital clock
- *
- * @param minutes
- * The new amount of minutes of this digital clock.
- * @post The new amount of minutes of this clock will be equal to the given minutes
- * if the given minutes are larger or equal to 0 and smaller than 60.
- * | new.getMinutes() == Math.abs(minutes%60)
- */
- public void setMinutes(int minutes) {
- this.minutes = Math.abs(minutes%60);
- }
- /**
- * Increments the time by one minute
- *
- * @post ...
- * | new.getMinutes() = (getMinutes() + 1)%60
- */
- public void incrementTime() {
- if (getMinutes() == 59) {
- setHours(getHours() + 1);
- }
- setMinutes(getMinutes() + 1);
- }
- /**
- * Checks whether clock1 is set to an earlier time than clock2.
- *
- * @return Returns true if clock1 is set to an earlier time than clock2.
- * @param clock1
- * The first clock.
- * @param clock2
- * The second clock.
- */
- public boolean isEarlierThan(DigitalClock clock1, DigitalClock clock2) {
- if (clock1.getHours() < clock2.getHours() || clock1.getMinutes() < clock2.getMinutes()) {
- return true;
- }
- return false;
- }
- private int hours;
- private int minutes;
- }
Advertisement
Add Comment
Please, Sign In to add comment