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: Simple Question about modular division

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Simple Question about modular division

    The program is supposed to output the following:
    Please Enter the Cost of the Item: 
    4.57 
    Please Enter the Amount Paid: 
    5.00 
    Change Owed: 0.43 
    Quarters: 1
    Dimes: 1
    Nickels: 1 
    Pennies: 3

    Instead, I keep getting:
    Please Enter the Cost of the Item: 
    4.57
    Please Enter the Amount Paid: 
    5.00
    Change Owed: 0.43
    Quarters: 0
    Dimes: 0
    Nickles: 1
    Pennies: 0

    Here is the code I'm using. Please help, thank you!!!
    import java.util.Scanner;
     
    public class change {
     
    	public static void main (String [] args){
     
    		System.out.println("Please Enter the Cost of the Item: ");
    		Scanner scan = new Scanner(System.in);
    		double cost = scan.nextDouble();
    		System.out.println("Please Enter the Amount Paid: ");
    		double paid = scan.nextDouble();
     
    		double owed = (paid - cost);
    		double owed_final = (double)Math.round(owed * 1000) /1000;
    		int quarters = (int)(paid / 25);
    		int dimes = (int)((paid % 25) / 10);
    		int nickels = (int)((paid % 25 % 10) / 5);
    		int pennies = (int) (paid % 25 % 10 % 5);
     
    		System.out.println("Change Owed: " + owed_final);
    		System.out.println("Quarters: " + quarters);
    		System.out.println("Dimes: " + dimes);
    		System.out.println("Nickles: " + nickels);
    		System.out.println("Pennies: " + pennies);
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple Question about modular division

    Try writing a small testing program that tries lots of modular math with different values so you see what the operator does.
    System.out.println( (x % y));
    change x and y to see the results.

    Copy the examples from your code also.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Simple Question about modular division

    Quote Originally Posted by Norm View Post
    Try writing a small testing program that tries lots of modular math with different values so you see what the operator does.
    System.out.println( (x % y));
    change x and y to see the results.

    Copy the examples from your code also.
    I did a couple of tests and I know how it works, I just can't seem to implement my knowledge of it into this program. Further help would really be appreciated Norm. Thank you.

    --- Update ---

    I changed paid to owed_final, since we're trying to find the change of what is left over, but it still gives me the same output sadly:
    import java.util.Scanner;
     
    public class change {
     
    	public static void main (String [] args){
     
    		System.out.println("Please Enter the Cost of the Item: ");
    		Scanner scan = new Scanner(System.in);
    		double cost = scan.nextDouble();
    		System.out.println("Please Enter the Amount Paid: ");
    		double paid = scan.nextDouble();
     
    		double owed = (paid - cost);
    		double owed_final = (double)Math.round(owed * 1000) /1000;
    		int quarters = (int)(owed_final / 25);
    		int dimes = (int)((owed_final % 25) / 10);
    		int nickels = (int)((owed_final % 25 % 10) / 5);
    		int pennies = (int) (owed_final % 25 % 10 % 5);
     
    		System.out.println("Change Owed: " + owed_final);
    		System.out.println("Quarters: " + quarters);
    		System.out.println("Dimes: " + dimes);
    		System.out.println("Nickles: " + nickels);
    		System.out.println("Pennies: " + pennies);
     
     
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple Question about modular division

    Which statement and expression is not giving the results you want?
    Add some println() statements to print all the results so you can see them and know which expression is wrong.
    Break the compound statements up into simple single statements an print the results of each.
    For example given: a/2*3
    change it to
    var = a/2
    print var
    var2 =var*3
    print var2
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Simple Question about modular division

    Quote Originally Posted by Norm View Post
    Which statement and expression is not giving the results you want?
    Add some println() statements to print all the results so you can see them and know which expression is wrong.
    Break the compound statements up into simple single statements an print the results of each.
    For example given: a/2*3
    change it to
    var = a/2
    print var
    var2 =var*3
    print var2
    There's something wrong with this:
    int quarters = (int) ((owed_final) / 25);
    int dimes = (int) ((owed_final % 25) / 10);
    int nickels = (int) ((owed_final % 25 % 10) / 5);
    int pennies = (int) (owed_final % 25 % 10 % 5);

    I think I get what you're saying by splitting them up, that's what I did with this and it worked:
    double owed = (paid - cost);
    double owed_final = (double)Math.round(owed * 1000) /1000;

    I split it up, but I can't get the calculations for how many quarters, dimes, nickels, and pennies to work the same way. I'm just not getting it I guess >>

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple Question about modular division

    What is printed out for each expression?
    Which expression is NOT returning the value you expect?
    Simple expressions contain only ONE operator. If there is a method call, it should take a variable, not an expression:
    Math.round(owed * 1000)
    vs
    var = (owed * 1000)
    var2 = Math.round(var)
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Simple Question about modular division

    Quote Originally Posted by Norm View Post
    What is printed out for each expression?
    Which expression is NOT returning the value you expect?
    Simple expressions contain only ONE operator. If there is a method call, it should take a variable, not an expression:
    Math.round(owed * 1000)
    vs
    var = (owed * 1000)
    var2 = Math.round(var)
    Been messing with the thing for an hour still not getting it, but believe me I tried. Do you think you could really do me a favor this one time and tell me how I can fix this so I can understand?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple Question about modular division

    Sorry, I would do exactly what you have been doing. You need to start at the very beginning and print out the results of each expression and compare what is printed with what you have manually computed to be the correct results.

    Post the current code and what is printed out when it is executed.

    Another compound statement that needs to be separated:
    int quarters = (int)(owed_final / 25);
    to
    double var = (owed_final / 25);
    PRINT var here
    int quarters = (int)var;
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Simple Division not working heeeeelp
    By ashboi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 9th, 2012, 05:48 PM
  2. Simple I/O Question...well could be simple for you?
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 30th, 2011, 06:44 AM
  3. [SOLVED] modular programming, converting to degrees
    By Actinistia in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 13th, 2011, 01:49 PM
  4. Magic square class (modular math problem)
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 30th, 2010, 10:56 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM