- 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 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;
}
}
- TestClockDisplay
public class TestClockDisplay
{
public TestClockDisplay()
{
}
public void test()
{
ClockDisplay clock = new ClockDisplay();
clock.setTime(12,30);
System.out.println(clock.getTime());
clock.setTime(21,45);
System.out.println(clock.getTime());
}
}
Dan ini hasil outputnya ^^