|
|
|
|
![]() ![]() |
Jan 11 2008, 09:27 AM
Post
#1
|
|
|
|||[ n00b King ]||| ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 715 Joined: 20-June 07 From: Auckland Member No.: 45,102 myCENT:20.20 |
Ok I've been doing some revision and came across this operator again. I've seen it before but havent fully understood what it does or its surpose to do.
Taken from "Objects first in java 2006" QUOTE The modulo operator The last method in the NumberDisplay class increments the display value by 1. It takes care that the value resets to zero when the limit is reached: public void increment() { value = (value + 1) % limit; } This method uses the modulo operator (%). The modulo operator calculates the remainder of an integer division. For example, the result of the division 27 / 4 can be expressed in integer numbers as result = 6 remainder = 3 The modulo operator returns just the remainder of such a division. Thus the result of the expression ( 27 % 4) would be 3. The example it gives (27 / 4) and the increment function to me dont seem to relate and this is where I'm confused as to why or how modulo is suppose to work. The increment function will increase 'value' by 1 everytime its run untill it reaches an int 'limit' then resets to zero. Well thats how it works but I'm still confused. For another reason the example is (27 / 4) where as the function would be ((lower number + 1) % limit ). A lower number on the left side % by limit which is a higher number. /sigh someone help explain this to me so I can figure this out. This post has been edited by sonesay: Jan 11 2008, 09:31 AM |
|
|
|
Jan 11 2008, 03:03 PM
Post
#2
|
|
|
apt-get moo ![]() Group: [MODERATOR] Posts: 2,237 Joined: 28-May 05 From: Devon, England Member No.: 7,593 ![]() myCENT:45.20 |
The example it gives (27 / 4) and the increment function to me dont seem to relate and this is where I'm confused as to why or how modulo is suppose to work. Modulo is what most people know as "child's division". When you are first taught to divide, you are told to give an answer like "6 remainder 2". Modulo returns the remainder. As far as I can tell, you understand this operation, and what is actually outputted when you use modulo? What you don't understand is how it is being used in the counter. The increment function will increase 'value' by 1 everytime its run untill it reaches an int 'limit' then resets to zero. Well thats how it works but I'm still confused. Lets go for an example. We'll start with value set to 0 and limit set to 7, OK? Good. Below I'll list what the code does and what output you'll get. Perhaps you'll spot the pattern and why it works:
For another reason the example is (27 / 4) where as the function would be ((lower number + 1) % limit ). A lower number on the left side % by limit which is a higher number. The lower number divided by a large number is shown in my example above. The large number goes into the small number 0 times, leaving a remainder equal to the small number. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 22nd November 2008 - 07:39 PM |