ANU The Australian National University



____________________________________________________

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

____________________________________________________

COMP2100/2500
Homework 7

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 6 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

This exercise is the second part of a three-part exercise to develop part of an automated cheque-writing program. Since the code will be re-used in Homework 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 integerAsWords(int n)

This way you'll be able to extract it easily next week. (The static is optional, 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 5 to count this (yet). I recommend that you keep your finished program for Homework 6 in a safe place and work on a copy. Then when you're finished, you can run 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 hw06/Words.java hw07/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-06/Words.java hw-07/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 5. Try it and see... We'll learn about how to do this sort of thing in the lectures on shell programming.


Hints

Keep your tested threeDigitsAsWords() function from Homework 6 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.

____________________________________________________

[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, 17:38:50 +1000
Feedback & Queries to comp2100@cs.anu.edu.au