Here is the problem:Change the clock from a 24 hour clock to a 12 hour clock. Be careful: this is not as easy as it might at first seem. In a 12 hour clock the hours after midnight and after noon are not shown as 00:30, but as 12:30. Thus the minute display shown values from 0 to 59, while the hour display shows values from 1 to 12!
The way of how i went to solve this problem was to change the actual parameter in the constructors (24 to 12).Then i modify the setTime() to accommodate the additional features. When hour == 12 implement hour.setValue(00),hour.setValue(30) and call the increment() functions.The problem is that when hour equals 12, the minutes don't display 30. Do i need to call the updateDisplay() within that block?
Thanks!
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // simulates the actual display
//clockdisplay constuctor
public ClockDisplay()
{
hours = new NumberDisplay(12);
minutes = new NumberDisplay(60);
updateDisplay();
}
//clockdisplay constuctor
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(12);
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 void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
}
/**
* Set the time of the display to the specified hour and
* minute.
*/
public void setTime(int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
//programmer made
if (hour >= 12)
{
hours.setValue(00);
minutes.setValue(30);
hours.increment();
minutes.increment();
}
updateDisplay();
}
/**
* Return the current time of this display in the format HH:MM.
*/
public String getTime()
{
return displayString;
}
/**
* Update the internal string that represents the display.
*/
private void updateDisplay()
{
//computer made
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
}