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.
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..
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?
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?
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)
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;
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 wordin 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 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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad: