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.

Results 1 to 16 of 16

Thread: Cant figure out how to moudulus!!!??

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Cant figure out how to moudulus!!!??

    Okay so basiclly my problem is I cant figure out how to properly modulus three variables. Since I am very new to java it would be much easier if I just post my problem!

    MessageBoxProgram_float.doc

    These are my directions ^^^^

    This is what I'm having trouble doing in the directions: Add the first 2 numbers and Modulus by the third number, store the result in fresult.

    And this is my code:

    //CISS-110-361
    //Jacob Lindsay
    //Coding Assignment 3 - Three Numbers
     
    import javax.swing.JOptionPane;
     
    public class MessageBoxProgram
    {
      public static void main(String[] args)
      {
        String snum1, snum2, snum3;                  //Variables
        float fnum1, fnum2, fnum3, fresult;
     
        //Get variables from user and store them
        snum1 = JOptionPane.showInputDialog("Please enter a deciaml number ");
        fnum1 = Float.parseFloat(snum1);
     
        snum2 = JOptionPane.showInputDialog("Please enter second deciaml number ");
        fnum2 = Float.parseFloat(snum2);
     
        snum3 = JOptionPane.showInputDialog("Please enter third deciaml number ");
        fnum3 = Float.parseFloat(snum3);
     
        //Perform calculations
        fnum3%= (fnum1 + fnum2);
        fresult = fnum3;
     
        //Display user input numbers
        JOptionPane.showMessageDialog(null,"Your numbers were: " + snum1 
                                        +", " + snum2
                                        +", " + snum3);
        //Show result of calculations
        JOptionPane.showMessageDialog(null,"The result is " + fresult );
        String.format("%.2f");
      }
    }

    Please I dont understand how to do the modulus on the two other numbers! I am so stumped its not even funny! This is an assignment due tonight so it would be greatly appreciated if someone could help me(The directions in the word file will help understand what im trying to do!)!


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    My Mood
    Dead
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant figure out how to moudulus!!!??

    Here is a resource that you could use that talks about modules Modules in the Java Language and VM

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cant figure out how to moudulus!!!??

    Thanks for the reply but that doesn't have anything to do with my problem I'm pretty sure

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Cant figure out how to moudulus!!!??

    Do you have any specific java programming problems?

    What output does the program generate? Add some println statements that print out the values of the variables so they can be copied and pasted here. Add some comments to the printout describing what is wrong and what the output should be.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cant figure out how to moudulus!!!??

    So I added some comments where I need help and an explanation. I posted my directions for writing the program if you look at that it will show what I need to do according to the modulus part of my problem!


    //CISS-110-361
    //Jacob Lindsay
    //Coding Assignment 3 - Three Numbers
     
    import javax.swing.JOptionPane;
     
    public class MessageBoxProgram
    {
      public static void main(String[] args)
      {
        String snum1, snum2, snum3;                  //Variables
        float fnum1, fnum2, fnum3, fresult;
     
        //Get variables from user and store them
        snum1 = JOptionPane.showInputDialog("Please enter a deciaml number ");
        fnum1 = Float.parseFloat(snum1);
     
        snum2 = JOptionPane.showInputDialog("Please enter second deciaml number ");
        fnum2 = Float.parseFloat(snum2);
     
        snum3 = JOptionPane.showInputDialog("Please enter third deciaml number ");
        fnum3 = Float.parseFloat(snum3);
     
        //Perform calculations
        fnum3%= (fnum1 + fnum2);         //HERE is where I am having problems!!!!!!
        fresult = fnum3;                //I am trying to get the modulus of fnum1 + fnum2 and it is only giving me the value of fnum1
                                        // Say I put in 8 for fnum1 10 for fnum2 and 18 for fnum3... it's supposed to  return the modulus(in this case it would be 0)
        //Display user input numbers       but instead it displays 8, the value of fnum1 which is incorrect
        JOptionPane.showMessageDialog(null,"Your numbers were: " + snum1 
                                        +", " + snum2
                                        +", " + snum3);
        //Show result of calculations
        JOptionPane.showMessageDialog(null,"The result is " + fresult );
        String.format("%.2f");
      }
    }

  6. #6
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Cant figure out how to moudulus!!!??

    Quote Originally Posted by jake25 View Post
    Thanks for the reply but that doesn't have anything to do with my problem I'm pretty sure
    the % modulus sign is used to get the remainder of a division, so if you divide 9 / 3 you get 0, if you divide 10/3 you get 3, but this isn't right, so if you do the division, and after that you do the same division but instead of a division sign you use % (10 % 3) you will get the remainder, which would be 1.

    Hope that helps!
    English is not my native language (Typo alert).

  7. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cant figure out how to moudulus!!!??

    Thank you for the information, I do know that it is the remainder I just cant seem to correctly place the modulus with the variables in my code to get the modulus of fnum1 + fnum2 by fnum3
    The step I need help with: Add the first 2 numbers(fnum1 & fnum2) and Modulus by the third number(fnum3), store the result in fresult.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Cant figure out how to moudulus!!!??

    Can you post the program's output and add some comments to it showing what it should be?

    Add the first 2 numbers(fnum1 & fnum2) and Modulus by the third number(fnum3), store the result in fresult.
    Break that into steps:
    Add the first 2 numbers(fnum1 & fnum2)
    Modulus by the third number(fnum3),
    store the result in fresult.

    What if the second step said:
    Divide by the third number(fnum3),
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cant figure out how to moudulus!!!??

    But wouldnt I need to declare another fresult? Sorry for my lack of understanding this, this is the hardest thing I have run into for some reason and I'm not sure why..
    //Perform calculations
        fresult = fnum1 + fnum2;
        fnum3%= fresult;
    I know this doesn't work because its telling me that variable fnum3 is not used and I cant add anymore variables to the code because thats not what the assignment asks to do.. So stumped on this

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Cant figure out how to moudulus!!!??

    Can you define another variable to hold the sum of the two variables done in the first step?

    How would you code it if the second step said:
    Divide by the third number(fnum3),
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cant figure out how to moudulus!!!??

    Yea I see what you're saying but my professor wanted us to do this with the variables he gave us. If I end up having to use an extra variable just to turn it in by tonight then I guess I'll have to do that but I feel like there's another way to do it that I'm not seeing..

  12. #12
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Cant figure out how to moudulus!!!??

    you can use () to avoid using fresult for what it is not meant for, fresult is only supposed to store the result of everything together, if you get that out of the way I think you'll be alot closer to getting it right >.<

    But,
    perhaps you should take a step back and not think in terms of coding it.
    You could try writing it down on paper first?
    English is not my native language (Typo alert).

  13. The Following User Says Thank You to Time4Java For This Useful Post:

    jake25 (September 29th, 2014)

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Cant figure out how to moudulus!!!??

    After you get the logic to work,
    then you can work on changing it to use the required variables.

    For example given these 3 statements
    x = a + b
    y = c/d
    result = x + y
    can be reworked by replacing x and y:
    result = (a+b) + (c/d)

    Get it to work and then change the expressions as per the assignment.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    jake25 (September 29th, 2014)

  16. #14
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cant figure out how to moudulus!!!??

    Yes I have tried to write it down on paper, thats one of the things my professor has us do. And can you explain a little more about using the () for fresult? I'm not sure what I would be using that for.

    --- Update ---

    I got it!!!

    //Perform calculations
        fresult = (fnum1 + fnum2) % fnum3;

    ..I feel like the biggest nub ever. But thank you guys for steering me in the right direction and making me figure it out myself instead of blatantly giving me the answer!

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Cant figure out how to moudulus!!!??

    Glad you got it.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #16
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Cant figure out how to moudulus!!!??

    You're welcome! You learn by doing
    English is not my native language (Typo alert).

Similar Threads

  1. Cant Figure it out. Can someone help me?
    By EricIsaiah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 28th, 2014, 02:17 AM
  2. I can't figure this out! Please help!
    By angyvill85 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2012, 03:36 AM
  3. [SOLVED] can't figure out..
    By darego in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 10th, 2010, 02:26 PM
  4. I'm new at this and can't figure it out
    By jaheh06 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 22nd, 2010, 08:44 AM