Advertisement
mmayoub

classes, clock class, Exercise 01 slide 46

Jul 7th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. package class170706;
  2.  
  3. /*
  4.  *Clock class
  5.  *
  6.  *Classes, Exercise 01, slide 46
  7.  */
  8.  
  9. public class Clock {
  10.     // properties
  11.     private int hours; // hours : 0 to 23
  12.     private int minutes; // minutes : 0 to 59
  13.     private int seconds; // seconds : 0 to 59
  14.  
  15.     // return hours property
  16.     public int getHours() {
  17.         return hours;
  18.     }
  19.  
  20.     // set hours property
  21.     // return true on success, false otherwise
  22.     public boolean setHours(int hours) {
  23.         // if invalid argument
  24.         if (hours < 0 || hours > 23) {
  25.             // do nothing and return false
  26.             return false;
  27.         }
  28.  
  29.         // update hours and return true for success
  30.         this.hours = hours;
  31.         return true;
  32.     }
  33.  
  34.     // return minutes property
  35.     public int getMinutes() {
  36.         return minutes;
  37.     }
  38.  
  39.     // set minutes property
  40.     // return true on success, false otherwise
  41.     public boolean setMinutes(int minutes) {
  42.         // if invalid argument
  43.         if (minutes < 0 || minutes > 59) {
  44.             // do nothing and return false
  45.             return false;
  46.         }
  47.  
  48.         // update minutes and return true for success
  49.         this.minutes = minutes;
  50.         return true;
  51.     }
  52.  
  53.     // return seconds property
  54.     public int getSeconds() {
  55.         return seconds;
  56.     }
  57.  
  58.     // set seconds property
  59.     // return true on success, false otherwise
  60.     public boolean setSeconds(int seconds) {
  61.         // if invalid argument
  62.         if (seconds < 0 || seconds > 59) {
  63.             // do nothing and return false
  64.             return false;
  65.         }
  66.  
  67.         // update seconds and return true for success
  68.         this.seconds = seconds;
  69.         return true;
  70.     }
  71.  
  72.     // increase the time by one second
  73.     public void tick() {
  74.         this.seconds += 1;
  75.  
  76.         if (this.seconds == 60) {
  77.             this.seconds = 0;
  78.             this.minutes += 1;
  79.  
  80.             if (this.minutes == 60) {
  81.                 this.minutes = 0;
  82.                 this.hours += 1;
  83.  
  84.                 if (this.hours == 24) {
  85.                     this.hours = 0;
  86.                 }
  87.             }
  88.         }
  89.     }
  90.  
  91.     // print a the clock on the screen as hh:mm:ss
  92.     public void show() {
  93.         System.out.printf("%02d:%02d:%02d", this.hours, this.minutes,
  94.                 this.seconds);
  95.     }
  96.  
  97.     // reset the clock to 00:00:00
  98.     public void reset() {
  99.         this.hours = this.minutes = this.seconds = 0;
  100.     }
  101.  
  102.     @Override
  103.     public String toString() {
  104.         return String.format("%02d:%02d:%02d", this.hours, this.minutes,
  105.                 this.seconds);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement