# [java] can any of you solve this?

This topic is 3504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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.)

##### Share on other sites
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?

##### Share on other sites
Quote:
 ...with the condition that this hasn't been done yet.

And how do you check this?

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by ahung89Thanks 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.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002290
×