OK, so I'm new to this and just signed up, but I need help with the explanations.
I have a task that asked me to convert a 24-hour clock to a 12-hour clock, making some changes. I am pretty sure that I'm almost there, however I cannot get the boolean to switch when the hour changes using the timeTick method in the code. Honestly, I believe that everything is in order, but any help will be appreciated:
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
private boolean isAM;
public ClockDisplay()
{
hours = new NumberDisplay(12);
minutes = new NumberDisplay(60);
updateDisplay();
setMorn();
}
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(12);
minutes = new NumberDisplay(60);
setTime(hour, minute);
setMorn();
}
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) {
hours.increment();
}
if (hours.getValue() == 12)
{
isAM = !isAM;
}
updateDisplay();
}
private void setMorn()
{
isAM = true;
}
private void setAft()
{
isAM = false;
}
public void setTime(int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}
public String getTime()
{
return displayString;
}
private void updateDisplay()
{
int hour = hours.getValue();
String daynight;
if (isAM = true)
{
daynight = "AM";
if (hour == 0)
{
hour = 12;
}
}
else
{
isAM = false;
daynight = "PM";
if (hour == 0)
{
hour = 12;
}
}
displayString = hour + ":" +
minutes.getDisplayValue() + daynight;
}
}
What we have
- Add a boolean field isAM that will be true (this morning) and false (this afternoon).
- We are creating two new methods; setMorning, which sets isAM to true, and setAfternoon, which sets isAM to false.
- , setMorning.
- timeTick , , am pm pm am. : isAM =! IsAM
- updateDisplay "am" "pm" , isAM .