Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. public class BoundedCounter {
  2.     private int value;
  3.     private int upperLimit;
  4.    
  5.     public BoundedCounter(int upperLimit) {
  6.         this.upperLimit = upperLimit;
  7.         this.value = 0;
  8.     }
  9.    
  10.     public void next() {
  11.         if (this.value < this.upperLimit) {
  12.             this.value++;
  13.         } else {
  14.             this.value = 0;
  15.         }
  16.     }
  17.    
  18.     public String toString() {
  19.         if (this.value < 10) {
  20.             return "0" + this.value;
  21.         } else {
  22.             return "" + this.value;
  23.         }
  24.     }
  25.    
  26.     public int getValue() {
  27.         return this.value;
  28.     }
  29.    
  30.     public void setValue(int number) {
  31.         if (number > 0 && number <= this.upperLimit) {
  32.             this.value = number;
  33.         }
  34.     }
  35. }
  36.  
  37. -----------------------------
  38.  
  39. public class Clock {
  40.     private BoundedCounter hours;
  41.     private BoundedCounter minutes;
  42.     private BoundedCounter seconds;
  43.    
  44.     public Clock(int hoursAtBeginning, int minutesAtBeginning, int secondsAtBeginning) {
  45.         // the counters that represent hours, minutes and seconds are created and set to have the correct initial values
  46.         BoundedCounter hours = new BoundedCounter(23);
  47.         hours.setValue(hoursAtBeginning);
  48.         BoundedCounter minutes = new BoundedCounter(59);
  49.         minutes.setValue(minutesAtBeginning);
  50.         BoundedCounter seconds = new BoundedCounter(59);
  51.         seconds.setValue(secondsAtBeginning);
  52.         //System.out.println(hours + " " + minutes + " " + seconds);
  53.     }
  54.    
  55.     public void tick() {
  56.         seconds.setValue(seconds.getValue() + 1);
  57.     }
  58.    
  59.     public String toString() {
  60.         return hours + ":" + minutes + ":" + seconds;
  61.     }
  62. }
  63.  
  64.  
  65. -------------------------------------
  66.  
  67. public class Main {
  68.     public static void main(String[] args) {
  69.         Clock clock = new Clock(23, 59, 50);
  70.        
  71.         System.out.println(clock);
  72.  
  73. //        int i = 0;
  74. //        while (i < 20) {
  75. //            System.out.println(clock);
  76. //            clock.tick();
  77. //            i++;
  78. //        }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement