COMP2100/2500
Homework 5 - larger numbers into words

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

Write the following program, following the initial PSP (for the last time) and filling in the Project Plan Summary and a Defect Recording Log.

Modify and extend your program from Homework 4 so that it can handle any positive integer (up to the largest positive integer on the system, which is 231 - 1 = 2147483647).

In all other respects the program should be unchanged. The only change is that the restriction to numbers in the range 1–999 is removed.

For example:

comp2100@partch java Words 2001
two thousand and one
comp2100@partch java Words 20000001
twenty million and one
comp2100@partch java Words 200030017
two hundred million, thirty thousand and seventeen
comp2100@partch java Words 2147483647
two billion, one hundred and forty-seven million, four hundred and eighty-three
thousand, six hundred and forty-seven

Write your program so that the conversion of integer to string is performed by a function

public (static) String integerAsWords(int n)

This way you'll be able to separate it easily for testing and extract it easily for use in other programs. (The keyword static is optional here, depending on how you design your program.)


Important PSP Note

The new thing this week is that we're modifying an existing program. This means that the size estimation and measurement must be of new and changed lines of code. You can't use your Linecount program from Homework 3 to count this (yet). I recommend that you keep your finished program for Homework 4 in a safe place (under a separate name/directory, and in SVN, for example) and work on a copy. Then when you're finished, you can run the Unix utility diff on the two finished programs to count the new and changed lines. If you use the ‘-u’ option for diff like this:

diff -u hw04/Words.java hw05/Words.java

then it is easy to pick out the new and changed lines: they will be the ones that start with a ‘+’. Remember not to count lines that only contain whitespace, braces or comment delimiters.

Alternatively, try this. I think it's correct:

diff -uw hw-04/Words.java hw-05/Words.java | grep '^+' | grep -v '^+++' | tr -d '+ \t{}*/' | grep . | wc -l

If I'm right, this tells you the number of new and changed lines, according to the rules in Homework 3. Try it and see... We'll learn about how to do this sort of thing in the lectures on shell programming. (the basic explanation: this line of commands runs diff, then uses grep to select only those lines in the output that start with a &lquo;+&rquo; character; and then does an inverse selection () of lines without 3 plus signs, and then uses tr to delete all of the plus characters, spaces, tabs, curlies, asterisks, and slashes: grep . selects non-empty lines (by selecting lines with one or more characters on them); and then wc -l counts how many of these lines there are.


Hints

Keep your tested threeDigitsAsWords() function from Homework 4 so that you can use it in the implementation of integerAsWords. Roughly what you have to do is break the number up into three-digit groups (the billions, the millions, the thousands and the rest), and use threeDigitsAsWords to print each of these, followed by “billion”, “million” or “thousand”, with appropriate use of commas or “and”s in between. It's the details of exactly when to use a comma and when not to, when to put “and” and when not to, when to leave something out etc. that makes this a bit tricky. You should be working out all these details in your design phase.


Copyright © 2005, 2009 Ian Barnes, Chris Johnson, The Australian National University
$Revision: 1.4 $ $Date: 2009/03/11 00:13:58 $
Feedback & Queries to comp2100@cs.anu.edu.au