1. Member
Join Date
Nov 2007
Posts
11
Rep Power
0

## help with recursion

i have exam on Monday and i can't solve some of exam problems.

exp1.

Write a recursive void method that takes a single(positive) int argument n and writes the integers 1,2,.....,n to the screen.

can anyone explain to me how to solve this type of problems.

:confused:

2. Member
Join Date
Apr 2008
Location
USA
Posts
12
Rep Power
0
It goes like this.
Java Code:
```import com.blogspot.joelnkode.tutorial.java.*;

public class CoolCode extends NewApplication {
public void startHere() {
int n = 20;
new ApplicationUtils(this).
recurse("recursor", n).
minus(1).
until(0).
start();
ApplicationUtils.exit();
}
public void recursor(int n) {
new OutputUtils(n);
}
}```

3. Member
Join Date
Apr 2008
Posts
28
Rep Power
0
public void Output(int n)
{if(n<0)
return;
else
{Output(n-1);
System.out.println(n);}}

4. Member
Join Date
Nov 2007
Posts
11
Rep Power
0
Thanks.
can u explain how to write a main for this method.
i don't know how to call a static method in main to print random numbers.

5. Member
Join Date
Nov 2007
Posts
11
Rep Power
0
got it!!!!!!!

6. Do you have a book?

regards,
sukatoa

7. Member
Join Date
Nov 2007
Posts
11
Rep Power
0
yes i have a book, why???

8. Originally Posted by Nari
yes i have a book, why???
How about you go through it looking for the answer to your question? Your question is a very basic one... it's crucial that you learn these concepts or you will not go far in this profession.

9. Member
Join Date
Apr 2008
Posts
28
Rep Power
0
with all due respect, sometimes a straight answer is better than spending 3 hours looking for an answer in a book.

10. Originally Posted by rico16135
with all due respect, sometimes a straight answer is better than spending 3 hours looking for an answer in a book.

Joe, unless that's recursive by the creator of the library- it's not recursive at first glance. Review fireball's example for what a recursive method looks like.

11. Member
Join Date
Apr 2008
Posts
28
Rep Power
0
well, i can't disagree with anything you said :)

12. Member
Join Date
Nov 2007
Posts
11
Rep Power
0
actually we finished this chapter on Friday and i had a exam today morning, it was comprehensive exam from 13 chapters and believe me i didn't wanted to spend so much time on one problem. I wasn't able to solve few problems, i didn't know how to call static method in main ,otherwise i do know how to write a main.

but you guys r right this is really crucial time for me because this is a basic course and i need to learn these things.

oh and i am not a guy!!!!
Last edited by Nari; 04-22-2008 at 06:08 AM.

13. Originally Posted by Nari
actually we finished this chapter on Friday and i had a exam today morning, it was comprehensive exam from 13 chapters and believe me i didn't wanted to spend so much time on one problem.
Strange my friend. How did you going to complete the exam on 13 chapters, your question asking here in chapter one of Java. Anyway I wish you all the best for the exam.

Originally Posted by Nari
I wasn't able to solve few problems, i didn't know how to call static method in main ,otherwise i do know how to write a main.

What you have tried. May you already take your exam. It's ok. Do those questions again see what you can do for the next :).

Originally Posted by Nari
but you guys r right this is really crucial time for me because this is a basic course and i need to learn these things.
But you have finished the first chapter, ;) right?

14. Originally Posted by Nari
oh and i am not a guy!!!!
Unless your sex is mentioned in your profile(JavaTip- maybe we can add this?), it's going to be presumed you're male due to this being predominantly a male profession- I don't mean to offend; I'm very happy to see you here. Frankly, in this profession- the more women the better. ;)

Thanks for clearing that up! :)

15. Basic recursion:

Java Code:
```public Object recursiveAlg(Object o)
{
if( Some kind of ending sequence)
{
return o; // or something along these lines
}
else
{
this.recursiveAlg(o - 1);  // usually o is an int or if it is an array than
// it would be array[myArray.length - 1]
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•