Here is the problem:There are (at least) two ways in which you can make a 12 hour clock. One possibility is to just store hour values from 1 to 12. On the other hand, you can just leave the clock to work internally as a 24 hour clock, but change the display string of the clock display to show 4:23 or 4.23pm when the internal value is 16:23. Implement both versions.

The way i went to try to solve this problem was to modify the updateDisplay(). I came up with an error, which it has to do with the fields that i'm trying to use are unknown within that function. I tried to put some formal parameters in the function signature to solve this problem, but came up with another error. So, i took the formal parameters out. I'm not sure what I'm doing wrong or how to approach this.

Thanks!

public class ClockDisplay
{
    private NumberDisplay hours;
    private NumberDisplay minutes;
    private String displayString;    // simulates the actual display
    
    //clockdisplay constuctor
    public ClockDisplay()
    {
        hours = new NumberDisplay(24);
        minutes = new NumberDisplay(60);
        updateDisplay();
    }
    //clockdisplay constuctor
    public ClockDisplay(int hour, int minute)
    {
        hours = new NumberDisplay(24);
        minutes = new NumberDisplay(60);
        setTime(hour, minute);
    }

    /**
     * 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();
        }
        updateDisplay();
    }

    /**
     * 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);
        
        if (hour == 12)
        {
            hours.setValue(00);
            minutes.setValue(30);
            hours.increment();
            minutes.increment();
            updateDisplay();
        }
            
        //ex 3.31 i have to do an if statement
        //i have to modify it this method.
        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.
     * 
     * accommodate the updateDisplay to print 4:23 when the internal
     * value is 16:23 date 10-21-09
     */
    private void updateDisplay()   //int hour, int minute
    {
        //hours.setValue(hour);
        //minutes.setValue(minute);

        //computer made
        displayString = hours.getDisplayValue() + ":" + 
                        minutes.getDisplayValue();
        
        //programmer made    
        //error:Incomparable types:NumberDisplay and int
        if (hours == 16 && minutes == 23)
        {
            hour.setValue(4);
            mintues.setValue(23);
            hours.increment();
            minutes.increment();
        }        
    }
}

Your error message is telling you what has gone wrong with this line:

if (hours == 16 && minutes == 23)

Here you are trying to compare your variable hours which is a NumberDisplay object with the number 16, which is an int. That's like saying "If this apple equals this orange" - how do you compare an apple to an orange? What you need to do is compare the hours to the NumberDisplay object with the value 16 (if that's what you really want to do?) Same goes for minutes variable...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.