[java] can any of you solve this?

Started by
20 comments, last by ahung89 15 years, 4 months ago
Hey guys... pretty new to java here. I took one semester in school and now that it's winter break I've been studying on my own. I recently came across a really tough exercise (well, maybe not tough for y'all) on recursion that I've been wracking my brain but don't know how to solve. Here it is: "Write a recursive method called starString that accepts an integer as a parameter and prints to the console a string of stars (asterisks) 2 to the nth power long. For example: starString(0) should print * starString(1) should print ** starString(2) should print **** starString(3) should print ******** etc" I just can't think of any solution that doesn't involve using a for loop! Any help would be appreciated! Also any tips on solving future problems like this would be great... I need to learn to think like a programmer.
Advertisement
Quote:
can any of you solve this?

Yes.

Quote:
Write a recursive method ... I just can't think of any solution that doesn't involve using a for loop!

Have you ever used recursion before?

Try something simpler:

Write a recursive function to count from 5 down to 0.

When you have that done, see if you can apply the same techniques to the other problem.
Do you know what a recursion is (and how to use it)? If so, this exercise should be pretty simple.
Try googling java recursion and you'll get your answer.
public static void countDown(int n) {
if(n >= 0) {
System.out.print(n);
countDown(n - 1);
}
}

The way I solved most of the exercises so far that are like this is by calling the function again but subtracting a bit. But the thing is in the problem I mentioned I don't really see how this principal would apply, since I'm using an int. I guess I'll think a little harder though.
First of all, I assume you know what recursion is. If not, google it and read up on it.

If yes, here's a hint: when thinking about recursion, you should try to look for the result of the previous recursion step in the result of the following recursion step. If you notice a pattern like that, it should be pretty straightforward to write it down as a function.

Let me know if you come up with something, and we'll go from there.

As for "learning to think like a programmer", e.g. analytical thinking + knowing your tools, is something that only comes with practice. Lots of practice.
When you wrap your head around this problem, try solving the Hanoi towers problem with recursion. Once you figure that one out, you pretty much have recursion covered and one step closer to a good analytic mindset :)

EDIT: too slow, but most of it still applies
write a function that takes an int and prints 1 star when the input it receives is zero. Then write a function that prints two stars when it receives an input of 1(using the previous function that prints 1 star when it receives zero.) Now see if you can combine the two functions to give you the answer you seek.(Hint multiple recursive calls are allowed.)
Thanks for the tips guys. I'm thinking I have a base case that calls starString sending 2^n as the parameter, with the condition that this hasn't been done yet. And the other case just calling starString(n-1), and a print('*') after that. Am I on the right track?
Quote:
...with the condition that this hasn't been done yet.


And how do you check this?
I guess I could use a global boolean, there's gotta be an easier way though. Stonemetal thanks for the reply. I didn't read it though... I've changed my mind, I don't wanna give up on solving this myself yet.
Quote:Original post by ahung89
Thanks for the tips guys. I'm thinking I have a base case that calls starString sending 2^n as the parameter, with the condition that this hasn't been done yet.
Nope, you are making it too hard. Well you could but there is a simple recursive solution.
Quote: And the other case just calling starString(n-1), and a print('*') after that. Am I on the right track?


Seriously though try writing a function that works only for an input of zero. Then an input of one and only call the function for the zero case solution. no odd globals needed.

This topic is closed to new replies.

Advertisement