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:

Java 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:

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

Thank You.

2. Originally Posted by House
Java 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

3. Thanks, but I want to understand why does

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

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

Java 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

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

6. 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
Last edited by JosAH; 08-29-2010 at 06:11 PM.

7. 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

8. 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

9. Hi Jos

I found this:

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

Java 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
Last edited by House; 08-29-2010 at 11:38 PM.

10. I did some googling and I found this:

Java Code:
``` Timer timer = new Timer();
public void run() {
timeTick();
}
};

I've added that to my code, but it doesn't compile ! it gives an error saying { <identifier> expected }

:(

11. Senior Member
Join Date
Mar 2009
Posts
552
Rep Power
8
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.

12. 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 }

Java 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();
public void run() {
timeTick();
}
};

/**
* 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. :(

13. The problem is that you did not put the Timer code in a method. Try placing it in the constructor instead of between methods.

14. Hi Zack

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

Java 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();
public void run() {
timeTick();
}
};

}

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

cannot find symbol - class timer

Still stuck :( what am I doing wrong this time ? :(
Last edited by House; 08-31-2010 at 01:01 AM.

15. 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.

16. Hi Norm

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

Java 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();
public void run() {
timeTick();
}
};

}

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

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

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

Thanks

17. 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.

18. It WORKED !!!

Thanks Norm. I changed this:

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

Java Code:
`import java.util.*;`
And now it works.

Thank You, Thank You, Thank You. :D

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•