COP 2210 / Pestaina

advertisement
COP 2210
Assignment 2: Numbers in Words
Write a program that reads a non-negative integer number (4 digits maximum) and writes
that number in words.
Your program must contain two classes: Numbers and Test.
Class Numbers:
Private Instance int value;
Variables:
Constructor
Numbers(int n)
Public Methods: String toString()
Private
Methods:
(You may add
other private
methods as
necessary)
Class Numbers
The value of the integer number
Initializes the value of number to n
Returns the spelling of private member,
value
Returns number of ones in value
int getOnes()
Returns number of tens in value
int getTens()
Returns number of hundreds in value
int getHundreds()
Returns number of thousands in value
int getThousands()
String digitToString(int d) Returns the spelling of d (one digit)(i.e. zero,
one, two, …)
String hundredsToString() Returns the spelling of hundreds in the value
(i.e. one hundred, …, two hundreds, …)
Returns the spelling of thousands in the
String
value (i.e. one thousand, , two thousands, …)
thousandsToString()
Returns the spelling of tens and ones in the
String
value (i.e. zero, one, two, … ten, eleven, …,
tensAndOnesToString()
twenty, twenty one, … ninety nine)
The above methods should not duplicate the work of other methods of the class and
should call them if necessary.
The Test class contains only the main method shown below:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the an integer (4 digits max) --> ");
int value=input.nextInt();
Numbers n = new Numbers(value);
System.out.println(n.toString());
}
Sample input/output
The following table shows a sample input/output for the program.
input
5
11
154
504
1001
5101
7376
output
Five
Eleven
One hundred and fifty four
Five hundreds and four
One thousand and one
Five thousands and one hundred and one
Seven thousands and three hundreds and
seventy six
Note that the first letter of the output is capitalized. Singular and plural words for
“thousand” and “hundred” are used and the different parts of the number are connected
with “and”.
Turn in: Printed listings of all your .java source code and a sample output.
Download