Results 1 to 13 of 13
Thread: Using modulo in my program
 10202013, 10:58 PM #1Member
 Join Date
 Oct 2013
 Posts
 13
 Rep Power
 0
Using modulo in my program
Hello,
I am writing a program which basically creates a 3 letter lock, say CAT for example, then the lock can be opened, closed, reset, and letters can be set individually. I am using only uppercase letters A  Z, (65  90 in unicode). The trick to this is to wrap the letters in a circle. Meaning after Z comes A, and before A comes Z. Just like a circular lock would have in real life. A lock can also be set with a tolerance, say the tolerance is 1, the lock combo of CAT can be opened with CAT, CBT, BAT, DAT etc.... the tolerance doesn't have a limit so it could be 5. Another example, a combination is ZAZ, and the tolerance is 5, this can be opened with ZAZ, AZA, ZAD, etc.. you get the point. I am having trouble with the wrapping of the code, I got the tolerance to work for most of the combos, but I am getting that this is the wrong combo when I try, the combo is ZAZ, the tolerance is 5, and ZAD will not open, which it should. Here is my method which determines whether the lock will open..
Java Code:public void pull() { isOpen = (((Math.abs(letter1  input1) % CHAR_RANGE) + (Math.abs(letter2  input2) % CHAR_RANGE) + (Math.abs(letter3  input3) % CHAR_RANGE)) <= tolerance); }
where inputx is the letter which was used as an attempt by the user to open the lock. (The combo attempted to open the lock)
where CHAR_RANGE is 'Z'  'A' which is 90  65 = 25.
and tolerance is the tolerance as explained in the paragraph above.
Anyways i have been over my head and tried nearly everything, I am still fairly new so if anyone can guide me in the right direction that would be great.
 10212013, 12:47 AM #2
Re: Using modulo in my program
Where are your notes that shows the values that the code returns when it is executed?
Make a spread sheet using some letters and write down the value returned by the  operator and then by the abs() method and then the value returned by the % operator.If you don't understand my response, don't ignore it, ask a question.
 10212013, 02:54 AM #3Member
 Join Date
 Oct 2013
 Posts
 13
 Rep Power
 0
Re: Using modulo in my program
Java Code:public void pull() { isOpen = (((Math.abs(letter1  input1) % CHAR_RANGE) + (Math.abs(letter2  input2) % CHAR_RANGE) + (Math.abs(letter3  input3) % CHAR_RANGE)) <= tolerance); }
Here is why it does not work;
Math.abs(Z  Z) = 0, 0 % 25 = 0.
Math.abs(A  A) = 0, 0 % 25 = 0.
Math.abs(Z  D) = (90  68) = 22, 22 % 25 = 22, when I want it to equal 4, since Z > A > B > C > D = off by 4.
I have no clue how to fix this. I've tried shifting all the letters down so A = 1 and Z = 26, like in the alphabet but then i can not wrap it properly so that after Z comes A and vice versa..
 10212013, 03:08 AM #4
Re: Using modulo in my program
Z > A > B > C > D = off by 4.
Given two characters: Z and D, are you saying the distance between them is 4. Is that what you are trying to compute: 4 given the chars: 'Z' and 'D'?
Why are there three letterN and three inputN (N=1 or 2 or 3)?If you don't understand my response, don't ignore it, ask a question.
 10212013, 03:41 AM #5Member
 Join Date
 Oct 2013
 Posts
 13
 Rep Power
 0
Re: Using modulo in my program
What Z and D demonstrate are the tolerance. in this case Z is the actual letter which is used to set the combo, in this case ZAZ, and since it is the last letter it would go into the variable letter3. Whereas D is the letter used to attempt to open the lock. In order to open the lock you need to input ZAZ, although since we set the tolerance to 5, the lock can be opened with ZAZ + 5 letters, so something like ZBA will open it, since Z and Z remain the same therefore tolerance = 0, A and B is Math.abs(6566)%25 which is 1, therefore tolerance is 1, and finally Z and A need also to be a tolerance of one since it is one letter off. I am not sure how else to describe this.
 10212013, 03:56 AM #6
Re: Using modulo in my program
If 'z' to 'd' is 22 and you want to get 4, what arithmetic can be done with the 22 to get 4?
When would that method be needed vs when getting the distance from 'a' to 'c'?If you don't understand my response, don't ignore it, ask a question.
 10212013, 04:22 AM #7Member
 Join Date
 Oct 2013
 Posts
 13
 Rep Power
 0
Re: Using modulo in my program
'Z' TO 'D' could be done by doing the number of letters in the alphabet (26)... 26 % 22 = 4
Although this would not work if there was no tolerance, for example, A  A = 65  65 = 0, 26 % 0 = undefined.
this is my problem, one of my solutions work one way and not the other, and the other does vice verca..
 10212013, 04:35 AM #8
Re: Using modulo in my program
What about 26  22 = 4?
If you don't understand my response, don't ignore it, ask a question.
 10212013, 04:41 AM #9Member
 Join Date
 Oct 2013
 Posts
 13
 Rep Power
 0
Re: Using modulo in my program
I see what you are saying, but what if the letter is B and the input is also B. This would mean B  B = 0. And according to your equation, we must do 26  0 = 26. When realistically i need it to equal 0.
 10212013, 05:05 AM #10
Re: Using modulo in my program
There are two cases: 'z' to 'd' and 'a' to 'b'
They need to be handled differently.If you don't understand my response, don't ignore it, ask a question.
 10212013, 05:45 AM #11Member
 Join Date
 Oct 2013
 Posts
 13
 Rep Power
 0
Re: Using modulo in my program
I finally got it to work, thanks to your help. My final code for the pull method is this.
Java Code:public void pull() { int tolerance1 = Math.abs(letter1  input1); int tolerance2 = Math.abs(letter2  input2); int tolerance3 = Math.abs(letter3  input3); if (tolerance != 0 && tolerance1 > 13) { tolerance1 = 26  tolerance1; } if (tolerance != 0 && tolerance2 > 13) { tolerance2 = 26  tolerance2; } if (tolerance != 0 && tolerance3 > 13) { tolerance3 = 26  tolerance3; } isOpen = (tolerance1 + tolerance2 + tolerance3) <= tolerance; }
 10212013, 01:36 PM #12
Re: Using modulo in my program
I finally realized that if all the letters are on a ring, there are two distances between each two letters: one going clockwise and the other going counterclockwise. The sum of the two distances is 26.
If you don't understand my response, don't ignore it, ask a question.
 10222013, 04:46 AM #13Member
 Join Date
 Oct 2013
 Posts
 13
 Rep Power
 0
Similar Threads

Workout program progression  Ways to make my Java program shorter/smarter?
By LasseA in forum New To JavaReplies: 4Last Post: 11212012, 02:19 PM 
The modulo operator
By Blaedel in forum New To JavaReplies: 2Last Post: 10092011, 05:29 AM 
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04022011, 01:57 PM 
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03222011, 07:49 AM 
Modulo Headache
By House in forum New To JavaReplies: 17Last Post: 09012010, 04:29 AM
Bookmarks