Changing the watch tick in Java

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;


/**
 * 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

  • 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 .
+4
2

, , , Dragondraikk, . , , . 0 - 11 . 0 12 , updateDisplay , 0, :

 public void timeTick()
    {
     minutes.increment()
     if(minutes.getValue() == 0) {  // it just rolled over!
         hours.increment(); 
        }
     if (hours.getValue() == 0)
         {
         isAM = !isAM;   
        }   
    updateDisplay();
    }

, :) . , , - , :)

0

, :

if (isAM = true)

isAM true, true, else .

, :

if (isAM == true)

- :

if (isAM)
+4

All Articles