Hi, I need help on this assignment I have, the question asks to write a program that reads a non-negative integer number (4 digits maximum) and writes that number in words. it gives me:
Private Instance
Variables: int value; The value of the integer number
Constructor Numbers(int n) Initializes the value of number to n
Public Methods: String toString() Returns the spelling of private member, value
Private Methods:
(You may add other private methods as necessary)
int getOnes() Returns number of ones in value
int getTens() Returns number of tens in value
int getHundreds() Returns number of hundreds in value
int getThousands() Returns number of thousands in value
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, …)
String thousandsToString() Returns the spelling of thousands in the value (i.e. one thousand, , two thousands, …)
String tensAndOnesToString()
Returns the spelling of tens and ones in the value (i.e. zero, one, two, … ten, eleven, …, twenty, twenty one, … ninety nine)
Now this is what I have so far and I just want to know if im going about this the right way, and if I am how do I continue:
public class Numbers {
private int value;
public Numbers(int n) {
value = n;
}
@Override
public String toString() {
return (hundredsToString() + thousandsToStrings() + tensAndOnesToStrings());
}
private int getOnes() {
return (value % 10);
}
private int getTens() {
return (value % 100 / 10);
}
private int getHundreds() {
return (value % 1000 / 10);
}
private int getThousands() {
return (value / 1000);
}
private String digitToString(int d) {
if (d == 0) {
return ("zero");
} else if (d == 1) {
return ("one");
} else if (d == 2) {
return ("two");
} else if (d == 3) {
return ("three");
} else if (d == 4) {
return ("four");
} else if (d == 5) {
return ("five");
} else if (d == 6) {
return ("six");
} else if (d == 7) {
return ("seven");
} else if (d == 8) {
return ("eight");
} else if (d == 9) {
return ("nine");
} else {
return ("input parameter is more than one digit");
}
}
private String hundredsToString(){
int d = getHundreds();
if (d == 1)
return digitToString(d) + "Hundred";
else
}
private String tensAndOnesToStrings() {
throw new UnsupportedOperationException("Not yet implemented");
}
private String thousandsToStrings() {
throw new UnsupportedOperationException("Not yet implemented");
}
}