Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Java recursion

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java recursion

    Could someone help me understand this code? Thanks
    So the program will print out

    "Example 1: x = 20
    20 > 10, therefore ... f(20) = f(20 - 3) + 2 = f(17) + 2
    17 > 10, therefore ... f(17) = f(17 - 3) + 2 = f(14) + 2
    14 > 10, therefore ... f(14) = f(14 - 3) + 2 = f(11) + 2
    11 > 10, therefore ... f(11) = f(11 - 3) + 2 = f(8) + 2
    8 <= 10, therefore ... f(8) = -5
    f(20) = 3
    "
    How does the computer know to take -5 and + by 8? Thanks

    class RecursiveMethods
    {
        RecursiveMethods()          //default constructor
        {
        }
     
        public int fOf(int x)
        {
            if (x <= 10)                        //the base case
            {
                System.out.println(x + " <= 10, therefore ... f(" + x + ") = -5");
                return -5;
            }
            else
            {
                System.out.println(x + " > 10, therefore ... f(" + x + ") = f(" + x + " - 3) + 2 = f(" + (x -3) + ") + 2");
                return fOf(x-3) + 2;
            }
        }    
    }
     
    public class RecursionMethodTester
    {
        public static void main(String[] args)
        {
            int x;
            RecursiveMethods rMethods = new RecursiveMethods();
     
            System.out.println("---------------------------------");
            System.out.println("       f(x - 3) + 2    if x >  10");
            System.out.println("f(x) = ");
            System.out.println("       -5              if x <= 10");
            System.out.println("---------------------------------");
            System.out.println();
     
            x = 20;
            System.out.println("Example 1:  x = " + x);
            System.out.println("f(" + x + ") = " + rMethods.fOf(x));
            System.out.println();


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java recursion

    Time to take a piece of paper and pencil and play computer and step through the code statement by statement to see what the computer sees. At each statement write down the values of the variables as they are changed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java recursion

    How can I make the computer display what it the value it seeing? I believe the computer are seeing value of x which are 17, 14,11, and 8.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java recursion

    Playing computer: For example if the program had this statement:
    x = 24;
    You would record a value of 24 for x
    If later the program had this statement:
    x = x + 3;
    you would find where you recorded the value of x and add 3 to it making x's new value 27

    make the computer display what it the value it seeing
    Use the println statement:
    System.out.println("x="+x); // display the value of x
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    maple1100 (February 10th, 2013)

  6. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java recursion

    I understand it when it it is replacing the value but what I don't understand is how the program take -5 + 8 without me adding the statement. Does the recursion record the value +2 each time it occur and automatically add it at the end? Thanks

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java recursion

    Can you show the output from the println statements that you added that shows what you are talking about?
    Where is the -5 and the +8?
    Where is the +2?

    Do you understand that each time a method is called a new copy of its args variables is created with new values?
    When a method calls another method (or itself recursively) the current values of all its variables are saved. Those variables' values will be restored when execution returns from the called method.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    maple1100 (February 10th, 2013)

  9. #7
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java recursion

    I understand how the program work now with your explanation of what happen when a method call itself. Thanks you so much!

Similar Threads

  1. Replies: 1
    Last Post: August 5th, 2012, 09:28 PM
  2. Need help with Java recursion problem!
    By eyesackery in forum Java Theory & Questions
    Replies: 2
    Last Post: June 5th, 2012, 08:12 AM
  3. How do you employ recursion into a Java Boggle program?
    By techcom0 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 3rd, 2011, 08:13 AM
  4. Replies: 2
    Last Post: July 7th, 2011, 03:46 PM
  5. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM