Reputation: 55
OK, so i'm new to this and have just signed up however i need some help with explanations please..
I have a assignment which has asked me to convert a 24 hour clock into a 12 hour clock by making some adjustments. I am pretty sure I am almost there however I cannot get the boolean to switch when the hour changes using the timeTick method in the code. To be honest I believe the rest is fine but any help would be appreciated:
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
private boolean isAM;
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(12);
minutes = new NumberDisplay(60);
updateDisplay();
setMorn();
}
/**
* 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(12);
minutes = new NumberDisplay(60);
setTime(hour, minute);
setMorn();
}
/**
* 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();
}
if (hours.getValue() == 12)
{
isAM = !isAM;
}
updateDisplay();
}
private void setMorn()
{
isAM = true;
}
private void setAft()
{
isAM = false;
}
/**
* 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);
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()
{
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
Upvotes: 4
Views: 1079
Reputation: 55
OK, so i finally worked it out thanks to Dragondraikk who made me think. The issue was just as he suggested, well kind of. as I set the hours to 0 - 11 and tick over. I was not changing 0 into a 12 until the updateDisplay was run so it was actually a 0 and not a twelve:
public void timeTick()
{
minutes.increment()
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
if (hours.getValue() == 0)
{
isAM = !isAM;
}
updateDisplay();
}
This fixed the problem and now it works :) thank you everyone for all your help.I am also aware that I can provide feedback in the form of a rating, if someone can tell me how I am more than happy to do so :)
Upvotes: 0
Reputation: 65821
Your problem is almost certainly in the line that reads:
if (isAM = true)
This is actually setting isAM
to true
and the result of the expression is therefore also true
so the else
part will never be executed.
You probably meant:
if (isAM == true)
or - better still:
if (isAM)
Upvotes: 4