• 08-29-2010, 01:03 PM
House
Hi Guys

I'm currently working on a project from a book, in the project the aim is to create a Digital Clock Display (without the GUI). Here is the code for the { NumberDisplay } class:

Code:

```/**  * The NumberDisplay class represents a digital number display that can hold  * values from zero to a given limit. The limit can be specified when creating  * the display. The values range from zero (inclusive) to limit-1. If used,  * for example, for the seconds on a digital clock, the limit would be 60,  * resulting in display values from 0 to 59. When incremented, the display  * automatically rolls over to zero when reaching the limit.  *  * @author Michael Kolling and David J. Barnes  * @version 2008.03.30  */ public class NumberDisplay {     private int limit;     private int value;     /**     * Constructor for objects of class NumberDisplay.     * Set the limit at which the display rolls over.     */     public NumberDisplay(int rollOverLimit)     {         limit = rollOverLimit;         value = 0;     }     /**     * Return the current value.     */     public int getValue()     {         return value;     }     /**     * Return the display value (that is, the current value as a two-digit     * String. If the value is less than ten, it will be padded with a leading     * zero).     */     public String getDisplayValue()     {         if(value < 10) {             return "0" + value;         }         else {             return "" + value;         }     }     /**     * Set the value of the display to the new specified value. If the new     * value is less than zero or over the limit, do nothing.     */     public void setValue(int replacementValue)     {         if((replacementValue >= 0) && (replacementValue < limit)) {             value = replacementValue;         }     }     /**     * Increment the display value by one, rolling over to zero if the     * limit is reached.     */     public void increment()     {         value = (value + 1) % limit;     } }```
In the Source code of the class about ALL is Clear to me EXCEPT the { increment() } method. I just can't understand how this:

Code:

```  public void increment()     {         value = (value + 1) % limit;     }```
Increments the timer. I would greatly appreciate some clear help.

Thank You.
• 08-29-2010, 01:09 PM
JosAH
Quote:

Originally Posted by House
Code:

```  public void increment()     {         value = (value + 1) % limit;     }```
Increments the timer. I would greatly appreciate some clear help.

Suppose the value of 'limit' is 60 (as in the comments of your code). Also suppose the value of 'value' is 58; calling the increment() method sets the value of 'value' to (58+1)%60 == 59%60 == 59. Calling increment() again (and a current value of 'value' == 59) sets this member variable to (59+1)%60 == 60%60 == 0. Exactly as stated in the comments, i.e. the value can go from 0 up to 59 and back to 0 again.

kind regards,

Jos
• 08-29-2010, 01:57 PM
House
Thanks, but I want to understand why does

Code:

`59%60 = 59`
What on earth does the { % } do ?
• 08-29-2010, 02:17 PM
JosAH
Quote:

Originally Posted by House
Thanks, but I want to understand why does

Code:

`59%60 = 59`
What on earth does the { % } do ?

The % operator returns the remainder after the integer division of a/b so (a/b)*b+a%b == a. Integer division ignores any fractional part, e.g. 7/3=2 so 7%3 == 1. Just take a piece of paper and a pencil and maybe program a bit of Java and you'll see ...

kind regards,

Jos
• 08-29-2010, 05:40 PM
House
Thanks Very Much Jos. Now I understand it, and I've got to say. It is a very neat solution to solving the problem.
• 08-29-2010, 06:08 PM
JosAH
Quote:

Originally Posted by House
Thanks Very Much Jos. Now I understand it, and I've got to say. It is a very neat solution to solving the problem.

Good; just play with that operator a bit so it becomes a 'second nature' and doesn't have any secrets for you anymore. In discrete mathematics the modulo operator is a powerful operator, e.g. have you ever realized that for any prime number p that p!+1 can only be divided by numbers larger than p (if any)? Using the modulo operator it's a breeze to prove it ;-)

kind regards,

Jos
• 08-29-2010, 06:17 PM
House
At the moment that increment method is called manually by me every 60 seconds. What would be the easiest way to automate the execution of that method so that the time ticks automatically like a real clock.

Thanks
• 08-29-2010, 07:28 PM
JosAH
Quote:

Originally Posted by House
At the moment that increment method is called manually by me every 60 seconds. What would be the easiest way to automate the execution of that method so that the time ticks automatically like a real clock.

Thanks

Have a look at one of the Timer classes.

kind regards,

Jos
• 08-29-2010, 11:34 PM
House
Hi Jos

I found this:

Code:

```public void scheduleAtFixedRate(TimerTask task,                                 Date firstTime,                                 long period)```
Can I do something like the following:

Code:

```public void scheduleAtFixedRate(increment(),                                 60000,                                 60000)```
I read the Class Description on the Java website, but I'm still not sure what to put for the date firstTime ( I think it too, is suppose to be 1 minute i.e. 60000 milliseconds), also I'm not sure about putting the { increment } method in there either !! Am I close ? or way off ?

That 60000 is milliseconds. i.e. 1 minute

Thanks
• 08-29-2010, 11:51 PM
House
I did some googling and I found this:

Code:

``` Timer timer = new Timer();     TimerTask task = new TimerTask() {         public void run() {             timeTick();         }     };         timer.scheduleAtFixedRate(task, 60000, 60000);```
I've added that to my code, but it doesn't compile ! it gives an error saying { <identifier> expected }

:(
• 08-30-2010, 03:33 AM
Singing Boyo
If you copied that code verbatim, you'd have to change timeTick(); to increment(); The timer calls the run method of the TimerTask after the given delay(s), and then you need to call increment() to update the clock.
• 08-30-2010, 10:46 AM
House
Hi S Boyo

No I did not copy the code verbatim. The project has two classes, one called { NumberDisplay } and this one below called { ClockDisplay }.

Here is the Source Code for { ClockDisplay }

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 2008.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();     }             Timer timer = new Timer();     TimerTask task = new TimerTask() {         public void run() {             timeTick();         }     };         timer.scheduleAtFixedRate(task, 60000, 60000);     /**     * 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()     {         displayString = hours.getDisplayValue() + ":" +                         minutes.getDisplayValue();     } }```
As you can see the timeTick() method exists. But I can't figure out why the code is not complining. :(
• 08-30-2010, 07:12 PM
Zack
The problem is that you did not put the Timer code in a method. Try placing it in the constructor instead of between methods.
• 08-31-2010, 12:58 AM
House
Hi Zack

I just tried that, here is the code with the new adjustment:

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 2008.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);         autoTick();         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);         autoTick();         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();     }         public void autoTick()         {         Timer timer = new Timer();         TimerTask task = new TimerTask() {             public void run() {                 timeTick();             }         };                 timer.scheduleAtFixedRate(task, 60000, 60000);         }     /**     * 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()     {         displayString = hours.getDisplayValue() + ":" +                         minutes.getDisplayValue();     } }```
Now when I try and compile I get an error saying:

Quote:

cannot find symbol - class timer

Still stuck :( what am I doing wrong this time ? :(
• 08-31-2010, 02:14 AM
Norm
You don't show any import statements in the code you posted. Many classes are in packages that you need to have import statements for to allow the compiler to find the class definition.

BTW When you post error messages, please copy and paste ALL of the error message text. Don't leave parts out.
• 08-31-2010, 11:18 PM
House
Hi Norm

Sorry about that, I forgot the import statement. My code now has the import statement, but it still won't compile !

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 2008.03.30  */ import java.util.Timer; 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);         autoTick();         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);         autoTick();         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();     }         public void autoTick()         {         Timer timer = new Timer();         TimerTask task = new TimerTask() {             public void run() {                 timeTick();             }         };                 timer.scheduleAtFixedRate(task, 60000, 60000);         }     /**     * 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()     {         displayString = hours.getDisplayValue() + ":" +                         minutes.getDisplayValue();     } }```
The error I am getting is for this line:

Code:

`TimerTask task = new TimerTask() {`
And the Error message says:

Quote:

Cannot find symbol - class TimerTask
Can someone please tell me what I doing wrong :(

Thanks
• 09-01-2010, 12:00 AM
Norm
Many classes are in packages that you need to have import statements for to allow the compiler to find the class definition.

Back to the API doc and find the package.
• 09-01-2010, 03:29 AM
House
It WORKED !!!

Thanks Norm. I changed this:

Code:

`import java.util.Timer;`
To this:

Code:

`import java.util.*;`
And now it works.

Thank You, Thank You, Thank You. :D