This site requires JavaScript, please enable it in your browser!
Greenfoot back
thekidj
thekidj wrote ...

2013/9/29

ClockDisplay from BlueJ (couldn't find active forums on their site)

thekidj thekidj

2013/9/29

#
/**
 * The ClockDisplay class implements a digital clock display for a
 * European-style 24 hour clock. The clock shows hours and minutes. The 
 * range of the clock is 00:00 (midnight) to 23:59 (one minute before 
 * midnight).
 * 
 * The clock display receives "ticks" (via the timeTick method) every minute
 * and reacts by incrementing the display. This is done in the usual clock
 * fashion: the hour increments when the minutes roll over to zero.
 * 
 * James McInnis, Michael Kolling, and David J. Barnes
 * September 23rd, 2013
 */
public class ClockDisplay
{
    private NumberDisplay hours;
    private NumberDisplay minutes;
    private NumberDisplay seconds;
    private boolean isAM;            // true if it is AM; otherwise false for PM.
    private String displayString;    // simulates the actual display

    /**
     * Constructor for ClockDisplay objects. This constructor 
     * creates a new clock set at 12:00 AM.
     */
    public ClockDisplay()
    {
        hours = new NumberDisplay(13);
        minutes = new NumberDisplay(60);
        seconds = new NumberDisplay(60);
        updateDisplay();
        setTime(12,00,00,true, "Sunday");
    }

    /**
     * Constructor for ClockDisplay objects. This constructor
     * creates a new clock set at the time specified by the 
     * parameters.
     */
    public ClockDisplay(int hour, int minute, int second, boolean isTimeAM, String wDay)
    {
        hours = new NumberDisplay(13);
        minutes = new NumberDisplay(60);
        seconds = new NumberDisplay(60);
        setTime(hour, minute, second, isTimeAM, wDay);
    }

    /**
     * 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 (isAM == false)
            {
                isAM = true;
            }
            if (isAM == true)
            {
                isAM = false;
            }
        }
        updateDisplay();
    }

    /**
     * Set the time of the display to the specified hour and
     * minute.
     */
    public void setTime(int hour, int minute, int second, boolean isTimeAM, String wDay)
    {
        hours.setValue(hour);
        minutes.setValue(minute);
        seconds.setValue(second);
        isAM = isTimeAM;
        updateDisplay();
        if (hour < 1 || hour > 12)
        {
            System.out.println("This time is invalid.");
        }
        if (minute < 0 || minute > 59)
        {
            System.out.println("This time is invalid.");
        }
    }

    /**
     * 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()
    {
        displayString = hours.getDisplayValue() + ":" + 
        minutes.getDisplayValue() + suffix();
    }

    public String suffix()
    {
        if (isAM == true)
        {
            return "AM";
        }
        else
            return "PM";
    }
}
My clocks switches from AM to PM fine, but doesn't go from PM to AM. I also can't figure out how to implement the clock only changing AM to PM (and vice versa) when it's 11:59
danpost danpost

2013/9/29

#
I am not sure if your timeTick method is written correctly to do what you want. I would thing you would want something more like the following:
public void timeTick()
{
    minutes.increment();
    if(minutes.getValue() == 0) 
    {  // it just rolled over!
        hours.increment();
        if (hours.getValue()%12 == 0)
        {
            isAM = !isAM;
        }
    }
    updateDisplay();
}
thekidj thekidj

2013/9/29

#
danpost wrote...
I am not sure if your timeTick method is written correctly to do what you want. I would thing you would want something more like the following:
public void timeTick()
{
    minutes.increment();
    if(minutes.getValue() == 0) 
    {  // it just rolled over!
        hours.increment();
        if (hours.getValue()%12 == 0)
        {
            isAM = !isAM;
        }
    }
    updateDisplay();
}
Worked perfectly. Thanks a bunch!
You need to login to post a reply.