Results 1 to 13 of 13
  1. #1
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default 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 letterx is the letter which was used to set the lock. e.g. CAT, C = letter1, A = letter2, T = letter3. (The real lock combo)
    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

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

  3. #3
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default 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);
        }
    Ok so say we have the combo ZAZ, and we set the tolerance to 5. If we try the combo ZAD is should work since tolerance is 4 and 4 <= 5.

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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Using modulo in my program

    Z -> A -> B -> C -> D = off by 4.
    I am not sure I understand what you are computing.
    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.

  5. #5
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default Re: Using modulo in my program

    Quote Originally Posted by Norm View Post
    I am not sure I understand what you are computing.
    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)?
    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(65-66)%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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

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

  7. #7
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default Re: Using modulo in my program

    Quote Originally Posted by Norm View Post
    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'?
    '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..

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

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

  9. #9
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

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

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

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

  11. #11
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default 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;
               }
    And it works as intended. Thanks for the tips.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default 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 counter-clockwise. The sum of the two distances is 26.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default Re: Using modulo in my program

    Yep, i managed to overcome that issue.

Similar Threads

  1. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  2. The modulo operator
    By Blaedel in forum New To Java
    Replies: 2
    Last Post: 10-09-2011, 04:29 AM
  3. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  4. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  5. Modulo Headache
    By House in forum New To Java
    Replies: 17
    Last Post: 09-01-2010, 03:29 AM

Tags for this Thread

Posting Permissions

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