berikut source code tiap classnya:
- NumberDisplay
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;
}
}
- ClockDisplay
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) {
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();
}
}
-TestClockDisplay (untuk mengecek)
public class TestClockDisplay
{
public TestClockDisplay()
{
}
public void test ()
{
ClockDisplay clock = new ClockDisplay();
clock.setTime(22,30);
System.out.println(clock.getTime());
clock.setTime(10,30);
System.out.println(clock.getTime());
}
}
Sekian program untuk menampilkan jam saya, Terima kasih :D