ANU The Australian National University



____________________________________________________

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

____________________________________________________

COMP2100/2500
Homework 6

Continue filling in a new Time Recording Log and Weekly Time Use Summary each week.

Write the following program, following the initial PSP and filling in the Project Plan Summary and a Defect Recording Log.

Write a Java program called ‘Words’ that prints out an integer in words.

The program shall take exactly one command line argument, which should be a positive integer less than one thousand (in other words no more than three digits). If it gets the wrong number of arguments or if its argument isn't an integer, or if the argument is not in the range 1–999, it shall print a usage message and exit.

If the input is OK, it shall print out the value of its argument, written out in words.

For example:

comp2100@partch java Words 17
seventeen
comp2100@partch java Words 123
one hundred and twenty-three
comp2100@partch java Words 210
two hundred and ten
comp2100@partch java Words 600
six hundred
comp2100@partch java Words 70
seventy
comp2100@partch java Words 999
nine hundred and ninety-nine
comp2100@partch java Words 1000
Usage: words n (where 0 < n < 1000)

Notice the use of the word ‘and’ in the output. This program must write numbers in the Australian/British style (“one hundred and thirty-seven”), not the American style (“one hundred thirty-seven”).

This exercise is the first part of the development of an automated cheque-writing module, such as might be used in payroll software. In order to make it harder for crooks to change the value of a cheque, we want the system to write the amount out in words, as well as in digits. Most accounting/payroll systems don't do this, probably because the programmers were too lazy to work out how. We can do better. The logic is a little bit complicated, which is why we're going to do it in stages.

Since the code will be re-used in Homeworks 7 and 8, you will need to follow some design guidelines. Write your program so that the conversion of integer to string is performed by a function

public static String threeDigitsAsWords(int n)

This way you'll be able to use it easily next week when we extend this program to deal with numbers of any size.

(Actually that method doesn't have to be static. How you design your program is up to you. But you must write a reusable routine to convert numbers in the range 1–999 to words.


Hints

Try to avoid writing code like

switch (n) {
case 1: string = "one"; break;
case 2: string = "two"; break;
...

Instead store the words in an array of strings so that the item at index 1 is the string "one" and so on. Then you can replace that whole ugly switch with a single line something like “string = digits[n]”. You'll need one array for the one digit numbers, one for the ‘teens’ and one for the multiples of ten: twenty, thirty, forty etc.

Alternatively, instead of arrays, you might prefer to use HashTables like in the assignment code.

____________________________________________________

[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, Friday, 1 April 2005, 12:52:40 +1000
Feedback & Queries to comp2100@cs.anu.edu.au