Modulo Headache

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.

" It does not matter how slowly you go so long as you do not stop. ".... Confucius

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.

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

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 ;-)

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

" It does not matter how slowly you go so long as you do not stop. ".... Confucius

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.

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.

" It does not matter how slowly you go so long as you do not stop. ".... Confucius

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.

If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!