89.1k views
5 votes
Use JavaWrite a program that will simulate a change machine found at cash registers. Input the amount due and amount paid from the keyboard.Tell the user how much change is owed and number of quarters, dimes, nickels, and pennies in change a customer would receive.Pay special attention to roundoff error. Your program should use division and modular division. No if’s or loops may be used. You may assume all change will be a positive number with no more than two decimal places. If more than $1.00 is owed, the full value should be included in the number of quarters returned. For example, $2.50 should be returned as 10 quarters.Hint: Modular division is a great way to find the remainder of a division. Think about how you can use this to calculate the change that is left over after some coins are given.Sample Run:Please Enter the Cost of the Item:4.57Please Enter the Amount Paid:5.00Change Owed: 0.43Quarters: 1Dimes: 1Nickels: 1Pennies: 3

User Asaka
by
6.7k points

1 Answer

4 votes

Answer:

Here is the JAVA program:

import java.util.Scanner; //to accept input from user

public class Main { // class name

public static void main(String [] args) { // start of main method

Scanner input = new Scanner(System.in); // creates Scanner class object to take input from user

System.out.println("Please Enter the Cost of the Item: "); // prompts user to enter the cost of item

double itemCost = input.nextDouble(); //scans and reads the input value of item cost

System.out.println("Please Enter the Amount Paid: "); // prompts user to enter the amount paid

double amount = input.nextDouble(); //scans and reads the value of amount from user

int change = (int)(amount * 100 - itemCost * 100); //compute the remaining amount i.e. change

System.out.println("Change Owed: " + change / 100.0); // displays the owed change

int quarters = change / 25; // computes the value for quarters

change = change % 25; // compute the quarters remaining

int dimes = change / 10; // computes dimes

change = change % 10; // computes dimes remaining

int nickels = change / 5; // computes nickels

change = change % 5; // compute nickels remaining

int pennies = change; // computes pennies

System.out.println("Quarters: " + quarters); // displays computed value of quarters

System.out.println("Dimes: " + dimes); // displays value of dimes

System.out.println("Nickels: " + nickels); // displays value of nickels

System.out.println("Pennies: " + pennies); }} //displays value of pennies

Step-by-step explanation:

I will explain the program with an examples.

Suppose the user enters 4.57 as cost of the item and 5.00 as amount paid. Then the program works as follows:

Change is computed as

change = (int)(amount * 100 - itemCost * 100);

This becomes;

change = (int)(5.00 * 100 - 4.57 * 100)

= 500 - 457

change = 43

Now the change owed is computed as:

change / 100.0 = 43/100.0 = 0.43

Hence change owed = 0.43

Next quarters are computed as:

quarters = change / 25;

This becomes:

quarters = 43/25

quarters = 1

Now the remaining is computed as:

change = change % 25;

This becomes:

change = 43 % 25;

change = 18

Next the dimes are computed from remaining value of change as:

dimes = change / 10;

dimes = 18 / 10

dimes = 1

Now the remaining is computed as:

change = change % 10;

This becomes:

change = 18 % 10

change = 8

Next the nickels are computed from remaining value of change as:

nickels = change / 5;

nickels = 8 / 5

nickels = 1

Now the remaining is computed as:

change = change % 5;

This becomes:

change = 8 % 5

change = 3

At last the pennies are computed as:

pennies = change;

pennies = 3

So the output of the entire program is:

Change Owed: 0.43 Quarters: 1 Dimes: 1 Nickels: 1 Pennies: 3

The screenshot of the output is attached.

Use JavaWrite a program that will simulate a change machine found at cash registers-example-1
User Karel Kral
by
6.0k points