# help with recursion

• 04-19-2008, 07:31 PM
Nari
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:
• 04-19-2008, 08:31 PM
JavaLovenJoe
It goes like this.
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);         } }```
• 04-20-2008, 02:04 AM
fireball2008
public void Output(int n)
{if(n<0)
return;
else
{Output(n-1);
System.out.println(n);}}
• 04-20-2008, 03:47 AM
Nari
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.
• 04-20-2008, 05:34 AM
Nari
got it!!!!!!!
• 04-20-2008, 06:54 AM
sukatoa
Do you have a book?

regards,
sukatoa
• 04-20-2008, 05:42 PM
Nari
yes i have a book, why???
• 04-21-2008, 07:01 PM
CaptainMorgan
Quote:

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.
• 04-22-2008, 01:47 AM
rico16135
with all due respect, sometimes a straight answer is better than spending 3 hours looking for an answer in a book.
• 04-22-2008, 02:12 AM
CaptainMorgan
Quote:

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.
• 04-22-2008, 02:19 AM
rico16135
well, i can't disagree with anything you said :)
• 04-22-2008, 06:01 AM
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. 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!!!!
• 04-22-2008, 07:09 AM
Eranga
Quote:

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.

Quote:

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 :).

Quote:

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?
• 04-22-2008, 07:37 AM
CaptainMorgan
Quote:

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! :)
• 04-22-2008, 08:07 AM
Eranga
Here goes :) Better to change my aviator too ;)
• 04-24-2008, 09:13 AM
Zosden
Basic recursion:

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]     } }```