| | |
Write numbers to words
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
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:
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:
Java Syntax (Toggle Plain Text)
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?
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"?
java Syntax (Toggle Plain Text)
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"?
Last edited by Easter Bunny; Sep 12th, 2008 at 3:49 am.
oh, and when you use the "case" thing and your number is a double digit, then the last option would be "default".
check the example here:
http://www.tech-recipes.com/java_pro...g_tips668.html
check the example here:
http://www.tech-recipes.com/java_pro...g_tips668.html
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
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!
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
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.
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.
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:
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
the problem you have with getting the hundreds, can easily be solved by saying:
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
so would it be ok to say that
and this would be under the hundredsToString() method correct?
Java Syntax (Toggle Plain Text)
int i = getHundreds();
and this would be under the hundredsToString() method correct?
Last edited by cmatos15; Sep 15th, 2008 at 3:07 pm.
![]() |
Similar Threads
- C++ hints to write a program, please help (C++)
- need help converting numbers to words (VB.NET)
- Converting numbers to text equivalent (C#)
- Numbers to english problem (Java)
- logical error (displaying words in order of length) (C++)
- Can you please help me write this word count program in another way (Python)
- need help asap :) (Java)
- Converting numbers to words (C++)
- Need help with including C++ code in Word macro (C++)
Other Threads in the Java Forum
- Previous Thread: defining objects, variables and checking validations for java
- Next Thread: Having problems rotating Shapes in JPanel
| Thread Tools | Search this Thread |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class classes click client code compare component coordinates database detection doctype eclipse educational error exception file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool linux list loop map method methods mobile netbeans newbie nextline pong print problem program programming project projectideas recursion recursive replaysolutions rim scanner screen sell server set size sms sort sql string swing terminal threads tree user web websites windows





