Question

In: Computer Science

Add a verification step to confirm that the amount from the calculated bills and calculated coins...

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();

}

}

Solutions

Expert Solution

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.


Related Solutions

Everyone knows what money is. It is the collection of coins and bills that we carry...
Everyone knows what money is. It is the collection of coins and bills that we carry around in our pockets, purses. and wallets. Or at least that people often think that is what money is? In fact, “money” is merely a “social convention” as described by the text – Money is “what it does” rather than “what it is.” i.     (1) Briefly describe three (3) of the four (4) functions that must be present to make money, well, money, AND; (2)...
1. Bills and coins make up about __________.a) 50–60% of the total money supplyb)...
1. Bills and coins make up about __________.a) 50–60% of the total money supplyb) 5–10% of the total money supplyc) 100% of the money supplyd) 15–20% of the money supply2.Many theories of the reasons for the development of states suggest that __________.a. they are based on kinship or tribal groups and are an attempt to maintain the autonomy of such groups within specified boundariesb. they were formed to combine smaller states with similar cultures into larger groups to afford them...
Given a snack vending machine, assume the machine accepts bills and coins, after customer input money...
Given a snack vending machine, assume the machine accepts bills and coins, after customer input money and select items, the machine can calculate and dispose snack. a. Finish a class diagram of the snack vending machine using correct UML notations. b. Convert the class diagram above to Java code.
All of these are features of bills in QuickBooks Online except what? You can add the...
All of these are features of bills in QuickBooks Online except what? You can add the contents of a purchase order to a bill. You can add the contents of a customer estimate to a bill. You don't have to pay the bill until the bill due date. QuickBooks Online tracks when the bill is due .
A car was purchased for bills enterprice in the amount of 18,000: 20% was paid in...
A car was purchased for bills enterprice in the amount of 18,000: 20% was paid in cash and a promissory note was signed for the remaining balance how would I make a journal entry of this?
Author Anna Schwartz writes this about money: “The U.S. money supply comprises currency—dollar bills and coins...
Author Anna Schwartz writes this about money: “The U.S. money supply comprises currency—dollar bills and coins issued by the Federal Reserve System and the Treasury—and various kinds of deposits held by the public at commercial banks and other depository institutions such as savings and loans and credit unions.” (Schwartz, n.d) In this threaded discussion, complete the following: 1) Discuss the concept of money. First, define the functions of money and explain how currency meets these functions. 2) Historically, a number...
Please provide a step by step solution create a Vacation Budget worksheet Add at least 6...
Please provide a step by step solution create a Vacation Budget worksheet Add at least 6 other expenses related to your planned vacation (car rental etc.) enter an estimate of the cost of for each item Use a function to calculate the total estimated costs. g) Enter a function to calculate the average cost per day. h) Create a chart (you choose the type) in this same worksheet based on this trip to display the estimated costs. Add a title...
Answer questions step by step. Also, Add the number on a schedule. Leader Company has two...
Answer questions step by step. Also, Add the number on a schedule. Leader Company has two support departments (S1 and S2) and two producing departments (P1 and P2). Estimated direct costs and percentages of services used by these departments are as follows: Allocation of support departments S1 S2 P1 P2 S1              - 10% 40% 50% S2 20% - 50% 30% Direct costs £4,500 £8,000 £10,000 £15,000         Required: a. Prepare a schedule allocating the support department costs to the producing departments...
This is from my emergency management class. What is verification and why is it important to...
This is from my emergency management class. What is verification and why is it important to disaster response, especially when dealing with online reports / information sources?
There are 100 coins in a jar. Two players take turns removing anywhere from 1-10 coins...
There are 100 coins in a jar. Two players take turns removing anywhere from 1-10 coins from the jar. The player who empties the jar by removing the remaining coin(s) wins the game. To guarantee that you win the game, would you choose to move first or second, and what strategy would you follow?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT