Project 7 – Making Change
This problem is based on an everyday situation, but it makes a good, simple example for programming.
The task is to make change for an arbitrary purchase amount less than $50, using an optimal
combination of pennies, nickels, dimes, quarters, one's,
five's, ten's and twenties.
Here's an example:
I buy something for $15.37 and pay with a twenty dollar bill.
I should get $4.63 in change.
There are
lots of possible ways to pay me my $4.63, but you know I'm going to be upset if you give me, say 463
pennies.
By an “optimal” combination I mean the combination that results in my getting the fewest
total coins and bills.
So I don't want 3 ones, 4 quarters,
6 dimes and 3 pennies (unless I'm going to the
laundromat).
I want 4 ones, 2 quarters, 1 dime and 3 pennies.
That's the fewest bills and coins you can
give me that add up to $4.63.
This problem is rendered a little more interesting than it might seem initially when you try to state,
exactly and in the detailed language of a Javaprogram, exactly how to carry out something you can, as a
human, do almost without thinking.
The twists and turns may surprise you.
Learning Objectives
L2 - Primitive data types, arithmetic and relational operators.
L4 – Collections (e.g. List, Queue) and Arrays
L5 - Object-oriented principles using Java.
L7 – Exception Handling Methods
(if you do the extra credit portion)
Please see the notes about using an array or other collection class to hold the bill and coin
denominations, below.
How to get started
In our example, I need to give back $4.63 (a real number, by the way).
Suppose I divide this amount by
20 (the largest bill I give in change).
What will the result be?
Zero.