[Bluej-discuss] Clock display

Denis BUREAU Denis.Bureau at wanadoo.fr
Tue Oct 10 09:53:08 BST 2006


Keokg,

The message from the compiler is not very informative,
and this error wasn't caused by the absence of a ; .

The problem comes from that part of the line :
(hours.getDisplayValue - 12)()
because
(hours.getDisplayValue - 12)
can't be the name of a method, and
()
should come just after a method name to say
we want to call that method without any parameter.

So maybe you wanted to write :
(hours.getDisplayValue() - 12)

But another problem arises :
getDisplayValue() returns a String, not an int,
so you can't substract 12 from it.

You should use getValue() instead
(look at the NumberDisplay class).

And now, the whole exercise is not so simple,
because hours are never zero in the am/pm notation.

Denis.

At 09/10/2006 19:46, you wrote:
>Hello
>I am working with the clock display in Chapter 3 and was trying to 
>have it Change the Time from 24 to Twelve and Display PM
>I made this change and keep getting  a error that says ':' Expected
>This is the line of code that it tells me the ; is expected but I 
>have a ; at the end of the code.
>             displayString = (hours.getdisplayValue - 12)() + ":" + 
> minutes.getDisplayValue() + "PM" ;
>
>Here is all the code
>
>/**
>  * 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.
>  *
>  * @author Michael Kolling and David J. Barnes
>  * @version 2006.03.30
>  */
>public class ClockDisplay
>{
>     private NumberDisplay hours;
>     private NumberDisplay minutes;
>     private String displayString;    // simulates the actual display
>
>     /**
>      * Constructor for ClockDisplay objects. This constructor
>      * creates a new clock set at 00:00.
>      */
>     public ClockDisplay()
>     {
>         hours = new NumberDisplay(24);
>         minutes = new NumberDisplay(60);
>         updateDisplay();
>     }
>
>     /**
>      * 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(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);
>         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()
>     {
>         if (hours > 12){
>             displayString = (hours.getdisplayValue - 12)() + ":" + 
> minutes.getDisplayValue() + "PM" ;
>
>
>
>                     }
>             else {
>             displayString = hours.getDisplayValue() + ":" +
>                         minutes.getDisplayValue() +  "AM";
>
>}
>
>}
>     }



More information about the bluej-discuss mailing list