PBOD 5

1.Class number
  1. public class NumberDisplay
  2. {
  3.     private int limit;
  4.     private int value;
  5.    
  6.     public NumberDisplay(int rollOverLimit)
  7.     {
  8.         limit = rollOverLimit;
  9.         value = 0;
  10.     }
  11.  
  12.     public int getValue()
  13.     {
  14.         return value;
  15.     }
  16.    
  17.     public void setValue(int replacementValue)
  18.     {
  19.         if((replacementValue >= 0) &&
  20.                 (replacementValue < limit)) {
  21.                     value = replacementValue;
  22.                 }
  23.     }
  24.    
  25.     public String getDisplayValue()
  26.     {
  27.         if(value < 10) {
  28.             return "0" +value ;
  29.         }
  30.         else {
  31.             return "" +value ;
  32.         }
  33.     }
  34.    
  35.     public void increment()
  36.     {
  37.         value = (value + 1) % limit;
  38.     }
  39. }


2.Class clock
  1. public class ClockDisplay
  2. {
  3.     private NumberDisplay hours;
  4.     private NumberDisplay minutes;
  5.     private String displayString; //simulates the actual display
  6.    
  7.     public ClockDisplay()
  8.     {
  9.         hours = new NumberDisplay(24);
  10.         minutes = new NumberDisplay(60);
  11.         updateDisplay();
  12.     }
  13.    
  14.     public ClockDisplay(int hour, int minute)
  15.     {
  16.         hours = new NumberDisplay(24);
  17.         minutes = new NumberDisplay(60);
  18.         setTime(hour, minute);
  19.     }
  20.    
  21.     public void timeTick()
  22.     {
  23.         minutes.increment();
  24.         if(minutes.getValue() == 0) { //it just rolled over
  25.             hours.increment();
  26.         }
  27.         updateDisplay();
  28.     }
  29.    
  30.     public void setTime(int hour, int minute)
  31.     {
  32.         hours.setValue(hour);
  33.         minutes.setValue(minute);
  34.         updateDisplay();
  35.     }
  36.    
  37.     public String getTime()
  38.     {
  39.         return displayString;
  40.     }
  41.    
  42.     private void updateDisplay()
  43.     {
  44.         displayString = hours.getDisplayValue() + ":" +
  45.                         minutes.getDisplayValue();
  46.     }
  47. }


3.Class testclock display
  1. public class TestClockDisplay
  2. {
  3.     public void test()
  4.     {
  5.         ClockDisplay clock = new ClockDisplay();
  6.        
  7.         clock.setTime(11,39);
  8.         System.out.println(clock.getTime());
  9.        
  10.        
  11.         clock.setTime(07,34);
  12.         System.out.println(clock.getTime());
  13.        
  14.         clock.setTime(16,14);
  15.         System.out.println(clock.getTime());
  16.     }
  17. }

4.Relation



5.










Komentar

Postingan populer dari blog ini

FP PBO :Calendar viewer

UTS ,RK Covid-19

ATM study case.