943,649 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5324
  • Java RSS
Sep 11th, 2008
-1

Write numbers to words

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  1. public class Numbers {
  2.  
  3. private int value;
  4.  
  5. public Numbers(int n) {
  6. value = n;
  7. }
  8.  
  9. @Override
  10. public String toString() {
  11.  
  12. return (hundredsToString() + thousandsToStrings() + tensAndOnesToStrings());
  13.  
  14. }
  15.  
  16. private int getOnes() {
  17.  
  18. return (value % 10);
  19.  
  20. }
  21.  
  22. private int getTens() {
  23.  
  24. return (value % 100 / 10);
  25.  
  26. }
  27.  
  28. private int getHundreds() {
  29.  
  30. return (value % 1000 / 10);
  31.  
  32. }
  33.  
  34. private int getThousands() {
  35.  
  36. return (value / 1000);
  37.  
  38. }
  39.  
  40. private String digitToString(int d) {
  41.  
  42. if (d == 0) {
  43. return ("zero");
  44. } else if (d == 1) {
  45. return ("one");
  46. } else if (d == 2) {
  47. return ("two");
  48. } else if (d == 3) {
  49. return ("three");
  50. } else if (d == 4) {
  51. return ("four");
  52. } else if (d == 5) {
  53. return ("five");
  54. } else if (d == 6) {
  55. return ("six");
  56. } else if (d == 7) {
  57. return ("seven");
  58. } else if (d == 8) {
  59. return ("eight");
  60. } else if (d == 9) {
  61. return ("nine");
  62. } else {
  63. return ("input parameter is more than one digit");
  64. }
  65.  
  66. }
  67.  
  68. private String hundredsToString(){
  69.  
  70. int d = getHundreds();
  71. if (d == 1)
  72. return digitToString(d) + "Hundred";
  73. else
  74.  
  75. }
  76.  
  77. private String tensAndOnesToStrings() {
  78. throw new UnsupportedOperationException("Not yet implemented");
  79. }
  80.  
  81. private String thousandsToStrings() {
  82. throw new UnsupportedOperationException("Not yet implemented");
  83. }
  84. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008
Sep 12th, 2008
0

Re: Write numbers to words

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

java Syntax (Toggle Plain Text)
  1. private String getNumber( int i )
  2. {
  3. switch ( i )
  4. {
  5. case 0 : return "zero";
  6. case 1 : return "one";
  7. .
  8. .
  9. .
  10. }

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Easter Bunny is offline Offline
57 posts
since Jun 2005
Sep 12th, 2008
0

Re: Write numbers to words

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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Easter Bunny is offline Offline
57 posts
since Jun 2005
Sep 12th, 2008
0

Re: Write numbers to words

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008
Sep 12th, 2008
0

Re: Write numbers to words

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008
Sep 13th, 2008
0

Re: Write numbers to words

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. 
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Easter Bunny is offline Offline
57 posts
since Jun 2005
Sep 15th, 2008
0

Re: Write numbers to words

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008
Sep 15th, 2008
0

Re: Write numbers to words

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:

Java Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Easter Bunny is offline Offline
57 posts
since Jun 2005
Sep 15th, 2008
0

Re: Write numbers to words

so would it be ok to say that

Java Syntax (Toggle Plain Text)
  1. int i = getHundreds();

and this would be under the hundredsToString() method correct?
Last edited by cmatos15; Sep 15th, 2008 at 3:07 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008
Sep 15th, 2008
0

Re: Write numbers to words

yes, if you fix the getHundreds method to what i posted, then you will get the right number.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Easter Bunny is offline Offline
57 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: defining objects, variables and checking validations for java
Next Thread in Java Forum Timeline: Having problems rotating Shapes in JPanel





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC