• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Java Beginner Challenge Problem

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

I have recently started with a course in Java. So I am very much a beginner. We got an assignment and I am stuck at the last part. I would greatly appreciate any help on how to solve the program. I will post the challenge and my solution so far. The problem is that my the method Reverse doesn't work with the added 'for loop printing the leading zeroes in the right order. Also we haven't done arrays or any more advanced subjects yet. So I need to solve this with a for loop. I can get all the methods to work fine on their own. However when I try to add the leading zeroes in the right order it just doesn't work. Can someone please help.

Thank you very much in advance.

Lydia J van Vuuren

CHALLENGE:

Number To Words

Write a method called numberToWords with one int parameter named number.

The method should print out the passed number using words for the digits.

If the number is negative, print "Invalid Value".

To print the number as words, follow these steps:

Extract the last digit of the given number using the remainder operator.

Convert the value of the digit found in Step 1 into a word. There are 10 possible values for that digit, those being 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Print the corresponding word for each digit, e.g. print "Zero" if the digit is 0, "One" if the digit is 1, and so on.

Remove the last digit from the number.

Repeat Steps 2 through 4 until the number is 0.

The logic above is correct, but in its current state, the words will be printed in reverse order. For example, if the number is 234, the logic above will produce the output "Four Three Two" instead of "Two Three Four". To overcome this problem, write a second method called reverse.

The method reverse should have one int parameter and return the reversed number (int). For example, if the number passed is 234, then the reversed number would be 432. The method  reverse should also reverse negative numbers.

Use the method reverse within the method numberToWords in order to print the words in the correct order.

Another thing to keep in mind is any reversed number with leading zeroes (e.g. the reversed number for 100 is 001). The logic above for the method numberToWords will print "One", but that is incorrect. It should print "One Zero Zero". To solve this problem, write a third method called getDigitCount.

The method getDigitCount should have one int parameter called number and return the count of the digits in that number. If the number is negative, return -1 to indicate an invalid value.
For example, if the number has a value of 100, the method getDigitCount should return 3 since the number 100 has 3 digits (1, 0, 0).


HINT: Use a for loop to print zeroes after reversing the number. As seen in a previous example, 100 reversed becomes 1, but the method numberToWords should print "One Zero Zero". To get the number of zeroes, check the difference between the digit count from the original number and the reversed number.

NOTE: When printing words, each word can be in its own line. For example, numberToWords(123); can be:

One
Two
Three
They don't have to be separated by a space.

NOTE: The methods numberToWords, getDigitCount, reverse should be defined as public static like we have been doing so far in the course.
NOTE: In total, you have to write 3 methods.

NOTE: Do not add a main method to the solution code.

MY SOLUTION SO FAR

[code=java]

 
Marshal
Posts: 73984
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Which part is it that you are having difficulty with?
You can greatlyy simplify that method to print “onetwothree” for 123.
 
Lydia van Vuuren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell and thank you very much for your help.  

I'm not a 100% sure where exactly my problem is but I suspect it is in the following method. What happens is that the first method prints out the words for numbers but it is reversed. Then the second method reverse, reverses the numbers so that the words are printed out in the correct order. However if the number 100 gets reversed the zeroes fall away because it is an int and it prints only 'one'. Now we are suppose to add a for loop in order to print the zeroes for the number to read 'one zero zero'. And it is with this that I am having a problem. I've added a for loop in the reverse method but it prints out only one zero and also not in the right order. The zero is before the 'one' in the instance of the number being '100' and also it is just one zero instead of 2.



 
Lydia van Vuuren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would greatly appreciate you sharing the easier way with me for printing 'onetwothree' as well.
 
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your reverse method should not display anything, it should only do the calculation. The whole thing about reverse is about being able to correctly translate 100 to "one zero zero". That's just one way. This whole assignment is very prescriptive, in my opinion and deprives you of the one thing you need to learn if you're going to be a programmer: how to come up with a solution on your own. The assignment presupposes a specific solution when in fact, there is a better way to do this.
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a hint: it's up to you to decide how you concatenate strings together. That is, if you have "zero" and then you get a "one", there's no reason you can't do this:

I'll let you figure out how you can use this concept to solve your problem without needing a reverse() method.
 
Campbell Ritchie
Marshal
Posts: 73984
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can create an array containing the names of the numbers.
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can create an array containing the names of the numbers.


OP said they haven't studied arrays yet so that probably means they can't use them.
 
Lydia van Vuuren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for responding Junilu Lacar and thank you for the hint. I will certainly think about it. Although we need to follow instructions for this course even if there are better ways of solving the problem.
 
Lydia van Vuuren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell Ritchie. I'm looking forward to learning about arrays. I'm instructed though to use a for loop to print the zeroes out. I have written a method where I can print the zeroes out just fine and if I combine the methods in main method everything works just fine. However the course won't take the solution because it wants me to incorporate the for loop somehow in either one of the three methods already written. This is what I am struggling with - how to nest the for loop in one of the methods in order for it to print out the zeroes.
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lydia van Vuuren wrote:we need to follow instructions for this course even if there are better ways of solving the problem.


In that case, at least study what I did below to solve the problem (somewhat) with the code you've already written. All I've done is rearranged (refactored) your code a little bit to separate actions from calculations.

Keeping calculations separate from actions in your programs can help keep your code simpler and easier to understand.

The output of that program is this:

One
Zero
Zero

One
Five
Zero

One
Zero
Zero
Zero

Two
Six

One
Two
Six
Five
Two
Three
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you had been allowed to use arrays, the wordFor() method could be greatly simplified:
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll never understand the style of teaching where you're not allowed to use the best tools for the job. By not allowing you to use arrays it's like your teacher said "Here's some wood. In order to build a table, you have to first cut the wood. Since you haven't learned how to use a saw yet, don't use a saw. Here's the hammer we've been talking about so far. Now, go cut the wood with it. "
 
Campbell Ritchie
Marshal
Posts: 73984
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it is an exercise in using switches.
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Maybe it is an exercise in using switches.


So it's like saying "Let's practice using a hammer by trying to cut wood with it."
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lydia van Vuuren wrote:it wants me to incorporate the for loop somehow in either one of the three methods already written. This is what I am struggling with - how to nest the for loop in one of the methods in order for it to print out the zeroes.


Part of your struggle is because you're currently mixing action with calculation in your reverse() method. The reverse() method should just be calculation: you calculate the reverse of a given number. Don't try to print anything to the console in there. Rework your solution so that you're doing all of the actions (displaying to the console) in only one method. The other two methods should just calculate a result.
 
Campbell Ritchie
Marshal
Posts: 73984
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . Let's practice using a hammer by trying to cut wood with it."

Hammers are very good at cutting things. Fingers, for example.

I was only trying to defend the teacher; maybe the class were told, “this is a very bad way to use switch because arrays work better, but you are still going to have to use a switch.”
 
Lydia van Vuuren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jumilu Lacar... Thank you so so much. This is a great learning curve. I really appreciate it.
 
Lydia van Vuuren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also thank you very much Campbell Ritchie for your contribution. I'm very grateful.
 
Bartender
Posts: 4631
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lydia van Vuuren wrote:Jumilu Lacar... Thank you so so much. This is a great learning curve. I really appreciate it.


But don't forget to stick to the exercise, with reverse and digitcount, no matter how much that exercise is being bashed.
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:no matter how much that exercise is being bashed.


Well, when one is given a hammer...  ¯\_(ツ)_/¯

 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lydia van Vuuren wrote:
I'm not a 100% sure where exactly my problem is but I suspect it is in the following method.
...


Your suspicion is correct. Using only the code you wrote and following the advice I gave earlier to avoid mixing calculation and action in the reverse() method, I moved some of your code around so that the program performs actions (displaying to the console) in only one of the methods instead. The program does work after those changes as far as I can tell.

Basically, the idea is this:
0. Given a number (data)
1. Get the reverse (calculation)
2. Break down the reverse number and print the word for each digit (action)
3. Print out as many "Zero" as needed to account for leading zeroes in the reverse number based on the difference between number of digits in the original number vs in the reversed number (action + some calculation)
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic