CIT 591 Assignment 9: Name That Number
Fall 2011, David Matuszek

Purposes of this assignment

General idea of the assignment

Write a program that:

Sample code

import java.util.Scanner;
 
public class ScannerExample {
    Scanner scanner = new Scanner(System.in); // This is "global"
 
    public static void main(String[] args) {
        new ScannerExample().run();
    }
 
    void run() {
        String line;
        String numeral = "12345";
        System.out.print("numeral \"" + numeral + "\" -> " +
        Integer.parseInt(numeral) + "\n");
        do {
            System.out.print("Type something (*end* to quit): ");
            line = scanner.nextLine();
            System.out.println(line);
            System.out.println("First character is " + line.charAt(0));
        } while (!(line.equals("*end*")));
        System.out.println("Goodbye.");
    }
}

Details

Your program is to read in text from the keyboard. It then prints out the same text, except that it turns every integer number between 0 and 9999, inclusive, into words. For the purposes of this program, an "integer" is any sequence of one or more digits.

The program should end when it reads a line that is exactly equal to *end*.

We will be using unit tests. You know what that means: You have to have the required methods, with the required signatures, and they have to return precise results.

Do not make exceptions for special cases--there are too many of them. For example, 3:15 should be translated as three:fifteen, and 3.1416 should be translated as three.one thousand four hundred sixteen.

Do not mark any of the following functions as private--that would prevent our unit tests from calling them.

Do not use regular expressions ("regexs") unless both you and your partner fully understand them.

Here are the signatures of the required methods. (You may have additional methods if you wish.)

String numbersToWords(String text)
Replaces each sequence of digits in text with the corresponding English words.
String nameOf(int number)
Given any number in the range 0 to 9999, returns the word or words representing that number. Words in the string are separated by exactly one space; the returned string does not begin or end with a space.
String nameOfDigit(int number)
Given any number in the range 0 to 9, returns a single word representing that number (zero through nine).
String nameOfTensDigit(int number)
Given any number in the range 1 to 9, returns a word representing that number of tens. That is, this method returns one of the words ten, twenty, thirty, ..., ninety.
String nameOfTeens(int number)
Given any number in the range 10 to 19, returns a word representing that number. That is, this method returns one of the words ten, eleven, twelve, ..., nineteen.

The program we will use to grade your program is very picky. The words returned by each of your methods must be spelled correctly. The String must not begin or end with a space (or any other whitespace). If you don't follow these rules, it is an error, and you will lose points.

I have not talked about the switch statement in class. It isn't necessary for this assignment, but it makes some of the methods easier. You can use it if both you and your partner understand it. (There is a writeup in my Java Syntax Pages, if you are interested.)

A note about Eclipse. When you type into the Console area in Eclipse, your typing will go in the correct place (at the end of whatever is already there). Eclipse isn't very good about showing you a cursor in this area to indicate where your typing is going to go.

Grading:

Grading will be based on getting the output of each method exactly correct, as judged by our grading program. In addition, you are required to format your program according to Java standards (Eclipse will do this for you if you tell it to, but it's your responsibility to tell it to.) You are also required to choose good variable names, and to capitalize them according to Java conventions (Eclipse will not do this for you.)

You will have a partner for this assignment. The two of you should turn in one copy of your program. Each of your names should be in an @author tag in the Javadoc for the class, as well as being printed out when the program is run.

Due date:

Your program is due before 6am Friday, November 18 . Zip up the entire directory for this project, and submit via Sakai. As this is a pair programming assignment, only one of you should submit the assignment, with both your names prominently displayed, as described above. As always, only assignments submitted via Sakai will be accepted--do not send your program by email. A late penalty of 5 points per day (out of 100 points) will apply.