Simple Question about modular division

• November 24th, 2013, 04:04 PM
GMPoison
Simple Question about modular division
The program is supposed to output the following:
Code :

```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:
Code :

```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!!!
Code :

```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);   } }```
• November 24th, 2013, 04:16 PM
Norm
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.
Code :

`System.out.println( (x % y));`
change x and y to see the results.

Copy the examples from your code also.
• November 24th, 2013, 04:44 PM
GMPoison
Re: Simple Question about modular division
Quote:

Originally Posted by Norm
Try writing a small testing program that tries lots of modular math with different values so you see what the operator does.
Code :

`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:
Code :

```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);     } }```
• November 24th, 2013, 05:03 PM
Norm
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
• November 24th, 2013, 05:16 PM
GMPoison
Re: Simple Question about modular division
Quote:

Originally Posted by Norm
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:
Code :

```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:
Code :

```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 >>
• November 24th, 2013, 05:25 PM
Norm
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)
• November 24th, 2013, 07:42 PM
GMPoison
Re: Simple Question about modular division
Quote:

Originally Posted by Norm
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?
• November 24th, 2013, 08:04 PM
Norm
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;