Results 1 to 16 of 16
  1. #1
    Nari is offline Member
    Join Date
    Nov 2007
    Posts
    11
    Rep Power
    0

    Question help with recursion

    Please help me.
    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. #2
    JavaLovenJoe is offline Member
    Join Date
    Apr 2008
    Location
    USA
    Posts
    12
    Rep Power
    0

    Default

    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. #3
    fireball2008 is offline Member
    Join Date
    Apr 2008
    Posts
    28
    Rep Power
    0

    Default

    public void Output(int n)
    {if(n<0)
    return;
    else
    {Output(n-1);
    System.out.println(n);}}

  4. #4
    Nari is offline Member
    Join Date
    Nov 2007
    Posts
    11
    Rep Power
    0

    Default

    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. #5
    Nari is offline Member
    Join Date
    Nov 2007
    Posts
    11
    Rep Power
    0

    Default

    got it!!!!!!!

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Do you have a book?

    regards,
    sukatoa

  7. #7
    Nari is offline Member
    Join Date
    Nov 2007
    Posts
    11
    Rep Power
    0

    Default

    yes i have a book, why???

  8. #8
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by Nari View Post
    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.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  9. #9
    rico16135 is offline Member
    Join Date
    Apr 2008
    Posts
    28
    Rep Power
    0

    Default

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

  10. #10
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by rico16135 View Post
    with all due respect, sometimes a straight answer is better than spending 3 hours looking for an answer in a book.
    Indeed- this is not one of those times- he didn't even write an attempt at his problem. Regardless, his original question was answered, and if someone doesn't know how to write the most fundamental of methods(main) then how exactly are we helping by blurting out the answer? He'll learn more from reading from a book, tutorial or online source which may lead him to experimenting with the code until he sees the light. Far more beneficial than us just giving him the answer. Keep in mind, we're not obliged to provide answers- we don't get paid for this; we devote our personal time to assisting others... you might forgive us if we choose to believe that honest people want to make an honest effort, in which case odds are they will receive real answers to their questions. The questions he had are found in just about every beginner book on Java programming- even other language programming. If it's taking you 3 hours to find an answer in a book- then the problem is not with the book where the answer exists.

    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.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  11. #11
    rico16135 is offline Member
    Join Date
    Apr 2008
    Posts
    28
    Rep Power
    0

    Default

    well, i can't disagree with anything you said :)

  12. #12
    Nari is offline Member
    Join Date
    Nov 2007
    Posts
    11
    Rep Power
    0

    Default

    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.

    anyways thanks for your help.:o
    oh and i am not a guy!!!!
    Last edited by Nari; 04-22-2008 at 06:08 AM.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Nari View Post
    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 View Post
    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 View Post
    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. #14
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by Nari View Post
    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! :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  16. #16
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

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

Similar Threads

  1. Help With Recursion
    By andrew777 in forum New To Java
    Replies: 1
    Last Post: 03-29-2008, 12:51 PM
  2. recursion
    By kdeighan in forum New To Java
    Replies: 3
    Last Post: 01-25-2008, 09:48 PM
  3. Recursion
    By bozovilla in forum Advanced Java
    Replies: 3
    Last Post: 01-07-2008, 04:53 PM
  4. recursion
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 05:15 PM
  5. Help with recursion
    By scts102 in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 10:07 PM

Posting Permissions

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