简体   繁体   中英

Clock tick change in Java

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

  1. Add a boolean field isAM which will have the value true (it is morning) and false (it is afternoon).
  2. We create two new methods; setMorning which sets isAM to be true and setAfternoon which sets isAM to be false.
  3. Both constructors will initialise the time of day to be morning by default and so both invoke setMorning.
  4. In timeTick we need to check whether the hours rolled over, meaning it either changed from am to pm or from pm to am. Note the use of: isAM = !isAM
  5. In updateDisplay we need to create the suffix "am" or "pm" depending on whether isAM is true or false.

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)

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM