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...
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"...
Write the code to implement the concept of inheritance forVehicles. You are required to implement inheritance...
Write the code to implement the concept of inheritance forVehicles. You are required to implement inheritance betweenclasses. You have to write four classes in C++ i.e. one superclass, two sub classes and one driver class. Vehicle is the super class whereas Bus and Truck are sub classesof Vehicle class. Transport is a driver class which contains mainmethod. Detailed description of Vehicle (Super class): The class Vehicle must have following attributes: 1. Vehicle model 2. Registration number 3. Vehicle speed (km/hour)...
a java code In this lab you will implement an inorder traversal, and a search for...
a java code In this lab you will implement an inorder traversal, and a search for a 2-3-4 tree. The inorder traversal will print the contents (integers) of the tree, and the search method will return a boolean, indicating whether a given key was found in the tree. Examine the provided template. The main method is provided for you, as well as a template of the Node and 2-3-4 classes. The main method will read either "inorder" or an integer...
write a program to make scientific calculator in java
Problem StatementWrite a program that uses the java Math library and implement the functionality of a scientific calculator. Your program should have the following components:1. A main menu listing all the functionality of the calculator.2. Your program should use the switch structure to switch between various options of the calculator. Your Program should also have the provision to handle invalidoption selection by the user.3. Your calculator SHOULD HAVE the following functionalities. You can add any other additional features. PLEASE MAKE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT