Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 31

Thread: Need some math help

  1. #1
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need some math help

    Hidy ho everyone,

    I found this code on how to break apart a number and store it in an array online. Quite frankly, I still don't understand how it works.

    	int input = Integer.parseInt(JOptionPane.showInputDialog("Please enter a 4 digit number"));
     
            int[] numbers;
     
            numbers = new int[4];
     
            for (int i = 3; i > -1; i--)
            {
                numbers[i] = (input % 10);
                input = input /10;
            }

    now I'm using this code again in another homework program where the user has to input a 4 digit number and I have to break that number apart. This program is about encryption.

    What I have to do is replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer (copied and pasted from my ebook text).

    now I can add 7 to everything in the array, but I don't understand what its asking me to do with getting the remainder after dividing by 10. That's where I'm having my difficulty.
    Last edited by mattig89ch; October 10th, 2014 at 08:27 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Need some math help

    You question is how to find the remainder of dividing by 10? Refer to the modulus operator.

  3. #3
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some math help

    Wait, is that what I was doing already with my first for loop?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,638
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Need some math help

    Did you try it? What happened?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some math help

    i'm not sure. up in my first post I have a for loop with a % sign as the arathmetic operator. And a cursory google search shows that as the way to finding the remainder by dividing by 10.

    Is that what that does?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,638
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Need some math help

    To see how to use the modulus operator, write a very small program that uses it and print out the results. Experiment some by changing the numbers and running it again. Try it several times to see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some math help

    Trouble is, I don't understand the math behind it. Those google searches haven't helped that.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Need some math help

    5 % 10 = 5
    12 % 10 = 2
    27 % 10 = 7

    What do you not understand about "the math behind it"? A remainder is a remainder.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,638
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Need some math help

    Did you write the test program I suggested so you could see the results with different numbers?
    Greg has provided you with several samples of what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some math help

    oh, so its just a way of separating the right most number from the left most?

    edit: also, i'm not quite sure what to write for that test program.

    Would it be something like:

    int input, remainder

    scanner.in.input

    remainder = input % 10

    println("Input: " + input)
    println("Remainder: " + remainder)

    that's probably a lot of wrong code. I was just spit balling.

    2nd Edit:

    I think I'm getting the hang of this. I was using that % thing just to separate the right most number from the rest.

    I started messing around with the program I said above, just done correctly.

            Scanner in = new Scanner(System.in);
     
                int input = in.nextInt();
     
                int remainder = input % 10;
     
                System.out.println("Input: " + input);
                System.out.println("Remainder: " + remainder);

    It looks like that % operator doesn't actually affect the initial value of the number. Input stays the same, no matter what you put in there, and remainder is just the last digit of the number you enter.

    Now I have another question. That homework I have pasted above said I need to encrypt a 4 digit number via that math process. I have the individual digits stored in an array. My question is, can I combine those numbers back into a single 4 digit int?
    Last edited by mattig89ch; September 25th, 2014 at 03:55 PM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,638
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Need some math help

    what to write for that test program.
    Yes something like that with 2-4 lines. No Scanner needed.
    The print out should show you want the % operator does with 2 numbers. Don't always use % 10.
    Change all the numbers, compile and execute several times to see what the results are with different values.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Need some math help

    so its just a way of separating the right most number from the left most?
    Huh? No, it's the remainder resulting from the division of the left number by the right number. Changing my examples:

    Division Result
    5 / 10 = 0 remainder 5
    12 / 10 = 1 remainder 2
    27 / 10 = 2 remainder 7

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,638
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Need some math help

    The divisor does not have to be 10:
    3 % 5 = 3
    5 % 3 = 2

    Making a test program would allow you to try many combinations to see what % does.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some math help

    I could change the above program to have an int. A basic question for you all, is there a way to have output and input appear on the same line in the command prompt?

    For example, I'd like to say something like "Enter the number to be precentified: " and then have the input be right next to it.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,638
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Need some math help

    Don't print a new line ("\n") at the end of the print out will leave the cursor on the same line.
    Or use the print() method instead of the println() method.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some math help

    You guys have been a big help so far. Math has never been a friend to me.

    Now two last questions I have.

    Heres the code I have so far thanks to your help:
    	int input = Integer.parseInt(JOptionPane.showInputDialog("Please enter a 4 digit number"));
     
            int[] numbers;
     
            numbers = new int[4];
     
            for (int i = 3; i > -1; i--)
            {
                numbers[i] = (input % 10);
                input = input /10;
            }
     
            for (int i = 0; i < 4; i++)
            {
                int tempHolder;
                tempHolder = numbers[i] + 7;
                //System.out.println("Temp Holder: " + tempHolder);
                numbers[i] = tempHolder % 10;
                tempHolder = 0;
            }
     
            System.out.println();
            System.out.println("The Encrypted numbers are: " + numbers[0] + numbers[1] + numbers[2] + numbers[3]);

    All I need now is a way to reverse what I've done. And it would be nice (but not required) to have the last number be a single int instead of an array, but thats not really a requirement, just something I'd like to do if at all possible.

    Can my above operations be reversed?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,638
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Need some math help

    Can you make a simple list of the operations that the code is doing?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some math help

    sure

    First a little message box pops up asking the user for a 4 digit number.

    next I create a 4 member array.

    After that I run that 4 digit number through a for loop, breaking off the numbers, and storing them in the array (last element first).

    Then I run that array through another for loop, adding seven then doing the getting remainder through dividing by 10 (I have to do that in the instructions).

    That last part is a little glossed over, but that's overall what I do. If you like I can go into a little more detail on that.

    Finally I output the array in order from 0-3.

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,638
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Need some math help

    last part is a little glossed over
    Then it's not worth much when trying to design the code to write.

    Which part are you trying to reverse?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some math help

    fair enough

    that last part in slightly more detail.

    it enters the final for loop. I declare a local integer to temporarily hold a value, and initialize it to the iteration of the loop plus 7. Then I assign the iteration of the loop to the temp value of the remainder after dividing by 10 (hope I said that right). Finally I reset the value of the local temp variable to zero.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,638
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Need some math help

    That's a paragraph, not a list of steps.
    Something like this
    declare a local integer
    initialize it to the iteration of the loop plus 7
    assign the iteration of the loop to the temp value
    of the remainder after dividing by 10 ???? Not sure where this goes
    reset the value of the local temp variable to zero.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some math help

    Thats actually the entire thing loop right there.

    And I did the remainder after dividing by 10 thing because that's what the instructions said to do. Now I have to figure out how to reverse that. Technically it says to write another program to reverse it, but I'm 99% certain that he won't mind if I include the decryption in the same program.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,638
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Need some math help

    Can you correct my attempt at a list of the steps the code is doing now so we have something to work from towards reversing the steps.

    Perhaps if you made an example it would help>
    For example with the steps of 1) add 4 and 2) multiply by 7
    x = 2
    x = x+4 >> 6
    x = x*7 >> 42

    then the reverse would be
    From above x = 42
    x = x/7 >> 6
    x = x - 4 >> 2
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some math help

    not sure where your getting the 4 from. And what does >> mean? Is it like the % sign?

    But I can try to make an example of my steps up to this point.

    It may very well be that we simply can't reverse it, and I have to try from a different aproach.

    First the user inputs some numbers. Lets say 1234

    X=1234

    Y[4] (creates an array of way with 4 shelves/things to store numbers in)

    Y[0] = 1
    Y[1] = 2
    Y[2] = 3
    Y[3] = 4

    Next I add 7 to the numbers

    Y[0] = 1+7
    Y[1] = 2+7
    Y[2] = 3+7
    Y[3] = 4+7

    Then I do that get the remainder after dividing by 10 thing

    Y[0] = Y[0] % 10
    Y[1] = Y[1] % 10
    Y[2] = Y[2] % 10
    Y[3] = Y[3] % 10

    Finally I output the numbers in an odd way the instructions tell me to, swapping the first and third spots, and the second and 4th spots

    output Y[2] Y[3] Y[0] Y[1]

    Edit: spelling

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,638
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: Need some math help

    The >> pointed to the results that would be in x. It was not meant as a java operator.

    In your example what would be the values in the array at the end of each of the steps?
    What math steps would need to be done to "undo" the % operator? Make a list of numbers that are possible values in this program (7-16), apply % to them and then look at what would need to be done to get back the original numbers.
    For example:
    7 % 10 = 7 Nothing needed to restore 7
    16 % 10 = 6 add 10 to get 16
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Question about math.pow and math.sqrt
    By ggx7 in forum Java Theory & Questions
    Replies: 7
    Last Post: March 7th, 2014, 12:51 PM
  2. Math.E - what exactly is it?
    By RedCloudTsunami in forum Java Theory & Questions
    Replies: 2
    Last Post: July 10th, 2012, 12:24 AM
  3. [SOLVED] Help with Math.tan and Math.atan
    By Dr.Code in forum Algorithms & Recursion
    Replies: 6
    Last Post: July 2nd, 2012, 05:54 AM
  4. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  5. Math Drill
    By Java Neil in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 5th, 2011, 01:12 AM