ANU The Australian National University



____________________________________________________

[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]

____________________________________________________

COMP2100/2500
Homework 10

Continue 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 Lecture 19 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.

____________________________________________________

[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]

____________________________________________________

Copyright © 2005, Ian Barnes, The Australian National University
Version 2005.1, Wednesday, 4 May 2005, 07:05:14 +1000
Feedback & Queries to comp2100@cs.anu.edu.au