Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, you will write a change calculator. You will first get five bills and the corresponding amount paid for that bill (both doubles)

In this assignment, you will write a change calculator. You will first get five bills and the corresponding amount paid for that bill (both doubles) from the user, and then for each one you will print the optimal change in dollars, quarters, dimes, nickels, and pennies. Here is a sample run of the program:

This program should contain a single class (called Proj4) with a main method. Your program should compile and run in BlueJ, and should look exactly like the screenshot above when it runs, except for the values of the numbers.

Here are some additional requirements:

Your program should print the change using the most dollars and then the fewest total coins. This will mean youll first need to use as many dollars as you can, then as many quarters, then as many dimes, etc.

If you are not using a particular coin for change (for example, finding change for 99 above does not use any nickels) then you should not print out that coin in the results.

If there is just one of a particular coin in the minimal change, then you should print the singular version of that coin in the result (for example, penny instead of pennies)

If there is no change (as for 0 in the example above), you should print no change in the results

You can adjust the spacing of the optimal change by printing a tab character in front of each type of coin. To do this, put a \t in your print statement. It will print a tab instead of \t.

You must store the five bill values (the numbers from the user) in one array and store the five amount paid values in a second array. (So spot 0 in the bill array and spot 0 in the amount paid array will correspond to the same transaction.) You must loop through these arrays when calculating optimal change for each number.

Hints

If your array of bill amounts is called bills, and your array of amount paid values is called amountPaid, you can use a for-loop with i counting through the array positions, and calculate:

double change = amountPaid[i]-bills[i]; 

to find the change for each position. For example, if the bill at that position was $3.23 and the amount paid was $5.00, then you would calculate $1.73 as the change. Next, convert the change value to cents by multiplying by 100 and converting the result to an integer (int):

int cents = (int)(change*100); 

In our example, cents would now be 173. Now, you can find the dollars portion of the change as follows, by taking advantage of integer division:

int dollars = cents / 100; 

In our example, dollars would be 1 (since the decimal part of the division is dropped with integer division). Now we need to find the remaining portion of the change that we still need to calculate (after pulling out a dollar):

cents = cents % 100; 

In our example, cents would now be 73 (the change value that we still need to process after pulling out the dollar). Now you can repeat the same process to pull out quarters (worth 25 cents), dimes, and nickels. Whatever change is leftover at that point is your pennies.

Because of a rounding issue when you convert from a double to an integer that has to do with how double values are stored in Java, it is possible that your program will be off by one cent in its change calculation. This is OK, and you will not be counted off for it.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions