Question

In: Computer Science

Java-- For this homework you are required to implement a change calculator that will provide the...

Java--

For this homework you are required to implement a change calculator that will provide the change in the least amount of bills/coins.

The application needs to start by asking the user about the purchase amount and the paid amount and then display the change as follows:

Supposing the purchase amount is $4.34 and the paid amount is $20, the change should be:

1 x $10 bill

1 x $5 bill

2 x Quarter coin

1 x Dime coin

1 x Nickle coin

1 x Penny coin

Note that the $20 and the $1 bills are not printed because the change did not require them, meaning that you do not print a bill unless the amount to be provided from that bill is larger than zero.

Hint: as discussed in class, you will need to cast from double to integer and this is how we did it in class:

int amount = (int)(scn.nextDouble()*100); // converting form dollars to cents by multiplying by 100

Solutions

Expert Solution

Assumption : Available denominations for giving change are $20 , $10 , $5 , $1 , Quarter Coins , Dime Coins , Nickle Coins and Penny coins.

It is a greedy approach where we always try to give the change of largest possible denomination. The approach repeats until the remaining amount becomes zero. For an easier implementation , everything should be converted into cents.

Hence , $20 = 2000 cents , $10 = 1000 cents , $5 = 500 cents , $1 = 100 cents , Quarter = 25 cents , Dime = 10 cents , Nickle = 5 cents and Penny = 1 cent.

Here is the complete working code using the above conversions.

import java.util.*;

public class Change
{
     public static void main(String []args)
     {
        /* Purchase and Paid are values in terms of cents */
        int purchase , paid;
        Scanner sc = new Scanner(System.in);
        /* Type casting and then multiplying dollars by 100 */
        System.out.println("Enter Purchase Amount");
        purchase =  (int)(sc.nextDouble()*100);
        /* Type casting and then multiplying dollars by 100 */
        System.out.println("Enter Paid Amount");
        paid = (int)(sc.nextDouble()*100);
        
        /*Decreasing order of denominations is Cents */
        int arr[] = {2000 , 1000 , 500 , 100 , 25 , 10 , 5 , 1};
        /* Decreasing order of denominations in Dollars */
        String denominations[] = {"$20 bill" ,"$10 bill" ,"$5 bill" , "$1 bill" , 
                   "Quarter Coin" , "Dime Coin " , "Nickle Coin"  , "Penny Coin"};
        
        /* rem is the amount to be paid back in terms of cents */
        int i = 0 , rem = paid - purchase;
        
        while(rem>0)
        {
            int denomination = arr[i];
            /* How many denominations of ith type can be given*/
            int howMany = rem/denomination;
            /* update remaining cents*/
            rem = rem%denomination;
            /* If a type of denomination can be given */
            if(howMany>0)
                System.out.println(howMany + " X " + denominations[i]);
            i++;
        }
     }
}

Here is the output to sample output to given input


Related Solutions

Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
In this project, you are required to write the java program “IO.java” to implement integer operations...
In this project, you are required to write the java program “IO.java” to implement integer operations “+”, “−”, “*”. Specifically, your program reads operands from a file named “input.txt” (which will be manually placed under the directory of the program) as strings. Then your program converts strings to integers, and computes the addition, subtraction, and multiplication of the operands. Finally, your program creates a file named “output.txt” (under the directory of the program) and writes the results to the file....
java program You are required to implement a Patient Information System for a clinic according to...
java program You are required to implement a Patient Information System for a clinic according to the following specifications: Class #1 PatientRecords (Tester class with main): This class contains a full list of patients (array list) and it provides the following functionalities: Add patient by ID-Number Print all records Print the number of patients based on specific attributes such as: disease name, gender. ***** See examples below for the input format (use console) Class #2 Patient: This class contains personal...
for java, Behold, the power of interfaces! On this homework problem you'll implement a completely generic...
for java, Behold, the power of interfaces! On this homework problem you'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects...
How would you implement the plan and /or launch a compensation change? Provide a clear and...
How would you implement the plan and /or launch a compensation change? Provide a clear and effective roll out plan for the program (whether new and /or if you recommend changes) as well as possible methods to assess the effectiveness.
In this homework, you will implement a single linked list to store a list of employees...
In this homework, you will implement a single linked list to store a list of employees in a company. Every employee has an ID, name, department, and salary. You will create 2 classes: Employee and EmployeeList. Employee class should have all information about an employee and also a “next” pointer. See below: Employee Type Attribute int ID string name string department int salary Employee* next Return Type Function (constructor) Employee(int ID, string name, string department, int salary) EmployeeList class should...
Multithreading Assignment In this homework, you will implement a multithreaded solution to finding the sum of...
Multithreading Assignment In this homework, you will implement a multithreaded solution to finding the sum of 9,000,000 double values. Begin, by creating a method that creates an array (not an arrayList) of 9000000 double’s and populates each index with a random number. Create a class to hold the sum of all the numbers in the array. Protect access to that sum by using a ReentrantLock appropriately. This means that only methods in this class can access or modify the array...
Homework 3 Loop and Function (PYTHON) You are asked to develop a compensation calculator application for...
Homework 3 Loop and Function (PYTHON) You are asked to develop a compensation calculator application for a department store. At the department store, the compensation of sales staff consists of a base salary and a commission. The base salary is $5,000, and the commission rate is tiered based on sales amount as following: Sales AmountCommission Rate $0.01 – $5,000 8% $5,000.01 – $10,000 10% $10,000.01 and above12% For example, if the sales amount is $12,000, the commission is calculated as...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
In this homework you will implement a Library class that uses your Book and Person class...
In this homework you will implement a Library class that uses your Book and Person class from homework 2, with slight modifications. The Library class will keep track of people with membership and the books that they have checked out. Book.java You will need to modify your Book.java from homework 2 in the following ways: field: dueDate (private)             A String containing the date book is due.  Dates are given in the format "DD MM YYYY", such as "01 02 2017"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT