PBOD 5
1.Class number
2.Class clock
3.Class testclock display
4.Relation

5.
- public class NumberDisplay
- {
- private int limit;
- private int value;
- public NumberDisplay(int rollOverLimit)
- {
- limit = rollOverLimit;
- value = 0;
- }
- public int getValue()
- {
- return value;
- }
- public void setValue(int replacementValue)
- {
- if((replacementValue >= 0) &&
- (replacementValue < limit)) {
- value = replacementValue;
- }
- }
- public String getDisplayValue()
- {
- if(value < 10) {
- return "0" +value ;
- }
- else {
- return "" +value ;
- }
- }
- public void increment()
- {
- value = (value + 1) % limit;
- }
- }
2.Class clock
- public class ClockDisplay
- {
- private NumberDisplay hours;
- private NumberDisplay minutes;
- private String displayString; //simulates the actual display
- public ClockDisplay()
- {
- hours = new NumberDisplay(24);
- minutes = new NumberDisplay(60);
- updateDisplay();
- }
- public ClockDisplay(int hour, int minute)
- {
- hours = new NumberDisplay(24);
- minutes = new NumberDisplay(60);
- setTime(hour, minute);
- }
- public void timeTick()
- {
- minutes.increment();
- if(minutes.getValue() == 0) { //it just rolled over
- hours.increment();
- }
- updateDisplay();
- }
- public void setTime(int hour, int minute)
- {
- hours.setValue(hour);
- minutes.setValue(minute);
- updateDisplay();
- }
- public String getTime()
- {
- return displayString;
- }
- private void updateDisplay()
- {
- displayString = hours.getDisplayValue() + ":" +
- minutes.getDisplayValue();
- }
- }
3.Class testclock display
- public class TestClockDisplay
- {
- public void test()
- {
- ClockDisplay clock = new ClockDisplay();
- clock.setTime(11,39);
- System.out.println(clock.getTime());
- clock.setTime(07,34);
- System.out.println(clock.getTime());
- clock.setTime(16,14);
- System.out.println(clock.getTime());
- }
- }
4.Relation

5.
Komentar
Posting Komentar