COMP2100/2500
Homework 7: Making ChangeContinue filling in a new Time Recording Log and Weekly Time Use Summary each week.
Write the following program, following the enhanced PSP as described in lectures, and filling in the Project Plan Summary and a Defect Recording Log. Use one of the Project Plan Summary forms that doesn't have greyed-out sections.
Write a Java program called ‘Change’ that works out how to make change in coins for a given number of cents.
The program shall take exactly one command line argument, which should be an integer in the range 3–497. If the argument is out of range, the program should print an error message and stop.
If the argument is not a multiple of 5, the program shall first round it to the nearest multiple of 5.
The different types of coins you may use for making change are 5c, 10c, 20c, 50c, $1 and $2.
The program shall use a ‘greedy’ algorithm to choose between the many possible ways to make change. That is, it shall first use as many $2 coins as it can, then as many $1 coins as it can, then as many 50c pieces and so on.
For example:
comp2100@partch java Change 20 20c = 1 x 20c comp2100@partch java Change 85 85c = 1 x 50c + 1 x 20c + 1 x 10c + 1 x 5c comp2100@partch java Change 90 90c = 1 x 50c + 2 x 20c comp2100@partch java Change 385 $3.85 = 1 x $2 + 1 x $1 + 1 x 50c + 1 x 20c + 1 x 10c + 1 x 5c comp2100@partch java Change 497 $4.95 = 2 x $2 + 1 x 50c + 2 x 20c + 1 x 5c comp2100@partch java Change 498 Error: argument 498 is too large.Output should be formatted exactly as in the examples above.
Hints
Looks to me like you'll either need nested loops or recursion, or perhaps just one loop and some clever use of % and integer division. If I were you I'd probably store the values of the coins in order in an array or Vector, same with their string representations.
Extensions
Consider how to make this as easy as possible to convert to other countries' currency systems, where the coins and notes have diferent names and different values. Refactor your program as necessary to achieve this; create several different currency versions. Note the change of names of the "dollars" and "cents" components, as well as differences in the denominations of coins available and the minumum coin available (for example, some countries still have 1c coins; New Zealand no longer has a 20c coin, and has 10c as the minimum coin) and hence the minimum amount accepted by the program (for rounding up to the minimum coin).
Information sources: you could start with Wikipedia: this is common, widely published knowledge of the kind for which Wikipedia is likely to be a reasonably reliable source.
Extension step 2 (easy): include all versions in the one program to cater for a choice of currencies. Use the standard currency abbreviations such as USD, GBP, AUD, NZD [ISO 4217 Currency Names] to denote the currency as the first program argument. For example:
comp2100@partch java Change AUD 20 20c = 1 x 20c comp2100@partch java Change NZD 20 20c = 2 x 10c comp2100@partch java Change GBP 20 20p = 1 x 20p
Copyright © 2005, Ian Barnes, The Australian National
University
$Revision: 1.7 $ $Date: 2009/04/28 06:02:12 $ $Author: cwj $
Feedback & Queries to
comp2100@cs.anu.edu.au