• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with program spelling out a number between 1-999.  RSS feed

 
Alex Gallo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code given. The program is supposed to take a number between 1-999 that the user inputs, and spells it out. What's confusing me the most is the implementation of the Random methods the prof put in there.

 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Random is used as an example, as an implementation of those methods. But you have to erase that Random parts and should write your own program to get the relevant output. Try yourself..
 
Alejandro Loaiza
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for decimalDigit() i worked up:

switch ( index )
{
case 9: number /= 10;
case 8: number /= 10;
case 7: number /= 10;
case 6: number /= 10;
case 5: number /= 10;
case 4: number /= 10;
case 3: number /= 10;
case 2: number /= 10;
case 1: number /= 10;
case 0: return number % 10;
default: return 0;

I also erased the random there. Would this be correct coming from the Algorithm?
 
Alejandro Loaiza
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also worked on onesWord() and I got:

switch ( digit )
{
case 0: return "zero";
case 1: return "one";
case 2: return "two";
case 3: return "three";
case 4: return "four";
case 5: return "five";
case 6: return "six";
case 7: return "seven";
case 8: return "eight";
case 9: return "nine";
default: throw new RuntimeException("Non-Digit " + digit);
}
i erased the random there. would this be correct?
 
Alex Gallo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like it works (no errors), but that verse variable is still being brought up on the return.

Edit:

I removed the entire section of verse, but I dont know what I should set the return to for that part. If I do "words", then it gives me the correct digit in the ones, but that's it. (e.g 123 spells out just three)

Here is the new code: http://pastebin.com/u2KZumjN
 
Alex Gallo
Greenhorn
Posts: 4
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest concentrating on getting the method

correct. All other methods depend on the digits that this method returns.

Take for example the number 321.

Now, determine how we would get the first digit, 3. This would be invoked by

The algorithm to do this is given in the comment, so let's use it:

divide 321 by 10, the number of times to do this being equal to the index.

So here goes:

321 / 10 = 32 (integer division, so the fraction is cut off)
32 / 10 = 3

So, we've done it two times, and we get 3 as a result. And it's correct!

Now, try to figure out how we could get the '2' when we invoke:


(although I must say that in the given algortihm step 2) is rather unclear to me)

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you need step 2 is if you want say the second digit from your example you would have:


321 / 10 = 32 (integer division, so the fraction is cut off)

We've done it one time so now we get the remainder when divided by 10

32 % 10 = 2

and that's the number we return.
 
Alejandro Loaiza
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone explain:
private static int decimalDigit(int index, int number)
{

//ALGORITHM:
//Step 1: Divide number by 10, index times
//Step 2: Find the remainder of number from Step 1 with 10
//Step 3: Return the remainder from Step 2
switch ( index )
{
case 2: number /= 10;
case 1: number /= 10;
case 0: return number % 10;
default: return 0;
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone explain:

No, it's not correct.
See my earlier post (which carries on from Piet's post) explaining what to do.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the methods teenWord(int), tenWord(int), ... are there for return the String that gives the spelling to an input number. But you have done all of them in threeDigitSpelling method.
And in each case you have to write word+=reading but not word=reading. That is one mistake.
In early post, I told you to erase all the Random codings. But you have left one in threeDigitSpelling method. You have to remove that and you should return word in that method as it has the spelling of the number.
The array verse is also an demonstration only. It is not necessary to the software.
 
Alejandro Loaiza
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would applying switches to teenWord() and tensWord() still be beneficial?
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No..
Now there is no need of those methods.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alejandro Loaiza wrote:would applying switches to teenWord() and tensWord() still be beneficial?

No, and you're concentrating too much on code. So: StopCoding (←click).

My advice: Take several two- and three-digit numbers at random, write them out, and really look at them hard. What do they have in common? What don't they?

You're some way towards a solution, but you're not there yet; and some of what you have is using "brute force" rather than rational logic. There's nothing wrong with that as a 'first cut'; but I suspect you'll work out that there are ways to reduce the number of strings you actually need to do the job.

This is NOT a simple problem, so you really need to think before you code.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!