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");
    }
}

instead of doing an if...else thing, perhaps a "switch" will be better?

private String getNumber( int i )
{
    switch ( i )
    {
         case 0 : return "zero";
         case 1 : return "one";
         .
         .
         .
     }

just for some reason i find doing more than one if with an else rather tacky looking... but that's just my opinion.

what you could do to test whether you are on the right track with getting the numbers separated, is when you call getHundreds() or whatever, you can print it to the screen:

number = 1234
thousands = 1
hundreds = 2
etc.

because "value % 1000 / 10" would return 23.

do you have to convert "1234" to "one two three four" or "one thousand two hundred and thirty four"?

The number 1234 would have to be One thousand and two hundreds and thirty four. He wants the first letter in CAPs and then if the number is more than 1 in the 1000s or 100s position it has to be in plural (two hundred"s"). I will try the switch case, it's just that we havent gone that far in class yet so I havent seen that yet. thanks!

The number 1234 would have to be One thousand and two hundreds and thirty four. He wants the first letter in CAPs and then if the number is more than 1 in the 1000s or 100s position it has to be in plural (two hundred"s"). I will try the switch case, it's just that we havent gone that far in class yet so I havent seen that yet. thanks!

ok, you can worry about the caps when the program works. for now see if you can write out "one two three four".

when you have that, work at get the last two digits to thiry four. that's when you'll have to use the tensAndOnesToString() method and test the tens and ones individually.

you can test the first number and see whether you have to say thirty-something, forty-something, etc. change the program so that your output will be "one two thirty".

work in little steps. don't try to rewrite unix and make it work first time. :)

I think my main area of confusion is the hundredsToString() method and the others that follow, im not quite sure how to go about them?? Also can you please explain to me how to go about testing each method seperately to know im on the right track. thanks

all you need to do to test the methods, is to ask the user to give you a number, then you write that number out in words, but without saying thousands or hundreds. just tell the user he entered "one two three four".

the problem you have with getting the hundreds, can easily be solved by saying:

int i = value / 100 % 10;

value = 1234
then you divide it by 100 and now
value = 12
then you "mod" it by 10 (divide by ten and then it gives you the remainder) and now
value = 2

so would it be ok to say that

int i = getHundreds();

and this would be under the hundredsToString() method correct?

yes, if you fix the getHundreds method to what i posted, then you will get the right number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.