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 8 of 8

Thread: division

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default division

    How do I get numbers to divide themselves with no remainders?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: division

    Can you give us some examples of what exactly you mean?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: division

    Quote Originally Posted by ericgomez View Post
    How do I get numbers to divide themselves with no remainders?
    This code does only dividing numbers with decimals.

    double number4 = Math.floor(Math.random() * 13);
    JOptionPane.showMessageDialog(null,number4,null,JO ptionPane.
    INFORMATION_MESSAGE);
    double number5 = Math.floor(Math.random() * 13);
    JOptionPane.showMessageDialog(null,number5,null,JO ptionPane.
    INFORMATION_MESSAGE);
    String number7 = JOptionPane.showInputDialog("Enter result");
    double number6 = Double.parseDouble(number7);
    double result2 = Math.round(number4 / number5);


    if (result2 == number6) {
    JOptionPane.showMessageDialog(null,"correct",null,
    JOptionPane.INFORMATION_MESSAGE);
    }
    else
    JOptionPane.showMessageDialog(null,"wrong",null,
    JOptionPane.INFORMATION_MESSAGE);

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: division

    Quote Originally Posted by ericgomez View Post
    How do I get numbers to divide themselves with no remainders?
    What are you doing with this line:
    Math.floor(Math.random() * 13);

    Your posts on the forum show repeated use of the floor method, What trouble are you having with dividing integers?

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: division

    Quote Originally Posted by jps View Post
    What are you doing with this line:
    Math.floor(Math.random() * 13);

    Your posts on the forum show repeated use of the floor method, What trouble are you having with dividing integers?
    That line rounds a random number to the nearest whole number so I do not get a random number that is in a decimal form.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: division

    Why don't you just use the Random class?
    Improving the world one idiot at a time!

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: division

    Quote Originally Posted by ericgomez View Post
    That line rounds a random number to the nearest whole number so I do not get a random number that is in a decimal form.
    No, it doesn't.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: division

    How about starting with an exact, precise problem statement? (Do this before struggling with the ins and outs and variable types and other program considerations.)

    For example, I might want to create a program that does something like this:

    Problem statement:

    We are going to make a test for division of positive integers by positive integers where the quotient is an integer.

    Now, for the quotient to be an integer, we know that the numerator must be an integer multiple of the denominator.

    So...

    The denominator will be an integer 2, 3, ..., 9

    The numerator will be a multiple of this, where the multiplier is 2, 3, ..., 15

    (We are assuming that problems with denominator equal to 1 or problems with numerator equal to denominator are so trivial that we don't want to waste time on them.)
    Generate a sequence of problems "manually" (using your brain, not a program) that shows how a run might go.

    For a text-based example it might go something like this:
    I am going to present 5 fractions.
     
    You will be asked to calculate the quotient.
    The quotient will always be an integer.
     
    OK.  Here goes:
     
     
    The fraction is 18 / 6.  Enter the quotient : 3
    You entered 3.  The answer is 3
     
    The fraction is 98 / 7.  Enter the quotient : 12
    You entered 12.  The answer is 14
     
    The fraction is 63 / 7.  Enter the quotient : 9
    You entered 9.  The answer is 9
     
    The fraction is 24 / 4.  Enter the quotient : 6
    You entered 6.  The answer is 6
     
    The fraction is 45 / 3.  Enter the quotient : 15
    You entered 15.  The answer is 15
     
    Of the 5 problems, the number of correct answers was 4.

    Now, here's the Big Step:

    Then think (yes think) about how you would create such a problem set. Really. Think about how you created the problem set. If you didn't create the set, go back and do it now. Really. Do it. You don't have to write out all of the verbiage; just create a few problems "manually."

    If you can't be bothered to do this (if you think it's too much trouble), then goodbye. It was nice to meet you.

    On the other hand if you are still with us...

    Did you just "select" some "random" numbers (off the top of your head) and see whether their quotient was an integer? And then stumble around until you found some "random" integers that actually gave an integer quotient? Really?

    Well, here's how it might have been done. I express the methodology in pseudo-code that could even be used to generate a program.

    Decide on the number of problems that will be presented.
     
    Make a loop that does the following for that number of times:
    BEGIN LOOP
     
        Generate a random integer 2, 3, ..., 9.  This is the denominator of the fraction.
     
        Generate a random integer 2, 3, ..., 15.  This is the multiplier.
     
        Calculate the numerator: denominator times multiplier.
     
        (Note that since numerator equals denominator times multiplier, correct value of the
        quotient is equal to the value of the multiplier.)
     
        Prompt the user by printing out numerator and denominator, and asking the user to
        enter a value for the quotient.
     
        (If the user enters something other than an integer, or an integer that
        is less than or equal to zero, maybe let him/her try again.)
     
        Test to see whether the user's answer is correct and act accordingly.
     
    END LOOP
     
    Print a summary giving the user's score.

    Does that make sense? If it doesn't, then create your own problem statement and see whether you can come up with a test set of problems and some pseudo-code that might lead to implementation.


    Then, and only then, try to write some Java consistent with the pseudo-code.

    Note that no floating point arithmetic is required for a problem involving integers.

    Note, also, that there is a nextInt() method of the Random class that obtains an integer within a specified range from a random number generator.


    Bottom line: Computer programs don't always do things the same way that humans do, but having a clear idea of what the heck the program must do and thinking about how such a thing might be done is recommended before actually sitting down with a text editor and writing program statements.



    Cheers!

    Z

Similar Threads

  1. Division by Zero
    By mael331 in forum Java Theory & Questions
    Replies: 16
    Last Post: December 6th, 2011, 06:13 PM
  2. Variable Division Issues
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:57 PM
  3. [SOLVED] Performing Division with Double Variables
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 5th, 2011, 05:36 PM
  4. division problem
    By vgenopoulos in forum Java Theory & Questions
    Replies: 1
    Last Post: July 30th, 2010, 01:51 PM
  5. [SOLVED] Java program to write a code to detect divisibility of any number by 11
    By Java'88 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 11th, 2009, 10:41 PM