In: Computer Science
Add a verification step to confirm that the amount from the calculated bills and calculated coins is the same as the original amount (i.e., output conversion amount and compare the two amounts for equality). The output should be something like:
Actual conversion amount: (money amount entered)
The two amounts are the same!
I have the following code and would like help adding the above part to the end of the program please
import java.util.Scanner;
public class MoneyConversions
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double oneDollar, fiveDollars, tenDollars, twentyDollars;
double penny, nickel, dime, quarter;
double amount;
System.out.print("Please enter an amount between 0.00 and 100.00: ");
amount = scan.nextDouble();
twentyDollars = amount / 20;
amount = amount %20;
tenDollars = amount / 10;
amount = amount %10;
fiveDollars = amount / 5;
amount = amount %5;
oneDollar = amount / 1;
amount = amount %1;
quarter = amount / .25;
amount = amount %.25;
dime = amount / .1;
amount = amount %.1;
nickel = amount / .05;
amount = amount %.05;
penny = amount / .01;
amount = amount %.01;
System.out.println((int)twentyDollars + " twenty-dollar bills");
System.out.println((int)tenDollars + " ten-dollar bills");
System.out.println((int)fiveDollars + " five-dollar bills");
System.out.println((int)oneDollar + " one-dollar bills");
System.out.println((int)quarter + " quarters");
System.out.println((int)dime + " dimes");
System.out.println((int)nickel + " nickels");
System.out.println((int)penny + " pennies");
System.out.println();
}
}
import java.util.Scanner;
public class MoneyConversions
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double oneDollar, fiveDollars, tenDollars,
twentyDollars;
double penny, nickel, dime, quarter;
double amount,copy_amount;
System.out.print("Please enter an amount between 0.00
and 100.00: ");
copy_amount = amount = scan.nextDouble();
System.out.println();
twentyDollars = amount / 20;
amount = amount %20;
tenDollars = amount / 10;
amount = amount %10;
fiveDollars = amount / 5;
amount = amount %5;
oneDollar = amount / 1;
amount = amount %1;
quarter = amount / .25;
amount = amount %.25;
dime = amount / .1;
amount = amount %.1;
nickel = amount / .05;
amount = amount %.05;
penny = amount / .01;
amount = amount %.01;
System.out.println((int)twentyDollars + " twenty-dollar
bills");
System.out.println((int)tenDollars + " ten-dollar bills");
System.out.println((int)fiveDollars + " five-dollar bills");
System.out.println((int)oneDollar + " one-dollar bills");
System.out.println((int)quarter + " quarters");
System.out.println((int)dime + " dimes");
System.out.println((int)nickel + " nickels");
System.out.println((int)penny + " pennies");
//System.out.println((int)Math.round(penny) + " pennies ");
System.out.println();
// Verification step
double bill_val=0.0;
// Now we are calculating the bills
bill_val = +(int)Math.round(penny)*0.01;
//rounding the pennies to get an integer value and
calculating the total amount in pennies.
bill_val = + nickel*0.05;
// adding the amount in nickel to bill_val
//This continues till we add all the items to the
bill_val.
bill_val = + dime*0.1;
bill_val = + quarter*0.25;
bill_val = + oneDollar*1;
bill_val = + fiveDollars*5;
bill_val = + tenDollars*10;
bill_val = + twentyDollars*20;
//In java we can't directly compare two floats or
double values. If we directly compare two floats or decimals it
gives us wrong results.
//I have taken a double threshold and assigned 0.01 to
it.
double Threshold = 0.001;
// If the difference between the bill_bal and amount
user entered is less than 0.001 then the two values are considered
to be equal.
// If the difference between the bill_bal and amount
user entered is more than 0.001 then the two values are considered
to be unequal.
if((bill_val - copy_amount) < Threshold) {
System.out.println("The two amounts
are same! ");
}else {
System.out.println("The two amounts
are not same! ");
}
}
}
A small suggestion.
If we use the above the number of pennies that would be printed in some cases would be wrong. The following is an example.
In the execution , the number of pennies must be 2 but it printed 1 due to the type conversion.The "penny" variable holds 1.999999999999602. so when we convert this value to int it would lose the 0.999999 and only will be printed as 1.
So i would suggest you to use the Math.round() function on the "penny" and then convert it into integer as i had shown in one of the comments above.