starting foreach
Practice with:
Description
Write an application, CorrectChange
, that makes correct change for a given amount of money. Your program should prompt the user for an amount of money (dollars and cents) and then print out the correct change using the fewest possible number of $20 bills, $10 bills, $5 bills, $1 bills, quarters, dimes, nickels, and pennies.
Some important requirements of this task:
Some requirements and recommendations that might make your life easier:
One method that you must define and use is makeChangeWithOneDenomination(int total, String denominationName, int denomination)
that satisfies this contract. Note that the contract gives you several examples on how to test the method above.
You can use the Scanner class to read in the user's decimal number from the keyboard, as a double
.
You can assume that the amount entered by the user is a valid decimal number, (like 35.51 for example), indicating dollars and cents. You do not need to handle invalid inputs, for example negative numbers, non-numbers (like 2a34 ), numbers with fractional amount of cents (like 32.15485).
Your code will be simpler if you work strictly with cents, so we recommend that you convert the decimal number entered by the user into an integer (representing the equivalent amount in cents), and work with it. For example, the number 35.51 in dollars is equivalent to 3551 cents. Note that converting double
to int
can be tricky.
So, another method you should define is makeChange(int cents)
which calls repeatedly the method makeChangeWithOneDenomination()
every time with a different set of arguments.
Testing your program
For each input you test your program, calculate by hand the expected result. Then, check the expected against the produced result. Obviously, they should agree.
Think carefully about the set of inputs (testing cases) you will provide to your program to assert its correctness. For this particular program, one good testing case would be to run it with input $36.41. Can you tell why?
In general your inputs should be such that every method you wrote is tested (invoked) as your program runs.
In BlueJ, save the results of your testing produced in the "BlueJ: Terminal Window" into a file called makingChangeTest.txt
. (Options > Save to File...)
What to submit
It is a standard policy of this course that submissions that have not been signed (@author
) and dated (@version
) will not be graded.
Your Gradescope submission should contain only the following:
CorrectChange.java
filemakingChangeTest.txt
file that contains your testing results