Skip to content

Instantly share code, notes, and snippets.

@Rahandi
Last active October 5, 2017 14:02
Show Gist options
  • Save Rahandi/452be016e559d626878be35990331b4c to your computer and use it in GitHub Desktop.
Save Rahandi/452be016e559d626878be35990331b4c to your computer and use it in GitHub Desktop.
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // simulates the actual display
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at the time specified by the
* parameters.
*/
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
/**
* This method should get called once every minute - it
* makes the clock display go one minute forward.
public class NumberDisplay
{
private int limit;
private int value;
/**
* Constructor for objects of class NumberDisplay
*/
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
public int getValue()
{
return value;
}
/**
* Set the value of the display to the new specified
* value. If the new value is less than zero or over the
* limit, do nothing.
*/
public void setValue(int replacementValue)
{
if((replacementValue >= 0) &&
(replacementValue < limit)) {
value = replacementValue;
}
}
/**
* Return the display value (that is, the current value
* as a two-digit String. If the value is less than ten,
* it will be padded with a leading zero).
*/
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
/**
* Increment the display value by one, rolling over to zero if
* the limit is reached.
*/
public void increment()
{
value = (value + 1) % limit;
}
}
public class TestClock
{
public void test()
{
ClockDisplay clock = new ClockDisplay();
clock.setTime(21,01);
System.out.println(clock.getTime());
clock.setTime(20,38);
System.out.println(clock.getTime());
clock.setTime(01,01);
System.out.println(clock.getTime());
clock.setTime(0,15);
System.out.println(clock.getTime());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment