7 Replies - 545 Views - Last Post: 26 April 2009 - 07:20 PM Rate Topic: -----

#1 Hikaroshi  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 42
  • Joined: 26-April 09

Recursion help

Posted 26 April 2009 - 02:37 PM

I have no idea how to do recursion but apparently it's on the APCS test... I don't even know why they have recursion on it because I realized that not that many people even use it.

Well, I was working on some questions my teacher found on some site and I was having difficulty on this question:
public static void method0910(String s)
{
int n = s.length();
if (n > 0)
{
String temp = s.substring(0,n-1);
method0910(temp);
System.out.println(s);
}
}


Which of the following is printed as a result of the call method0910("TANGO") ?

I looked at the solution and it says it's TA TAN TANG TANGO... But, how? What does the 0,n-1 do? The ',' I never understood either. Can somebody please go over this for me?

Is This A Good Question/Topic? 0
  • +

Replies To: Recursion help

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Recursion help

Posted 26 April 2009 - 03:36 PM

The comma -- , -- is just used to separate arguments passed to the substring() method.

The 0 is the first argument. That is the argument of the index to start at. 0 means the first character in the string.

The n - 1 is what character index to go to. The first method call it will go to the last letter of the string. In the next call, it will go to the second to last character in the string, and so on, while the length of the string is greater than 0. The variable n changes with every call of the method, which is how the recursion actually operates.

This post has been edited by Locke: 26 April 2009 - 03:37 PM

Was This Post Helpful? 1
  • +
  • -

#3 Skyd  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 27
  • Joined: 09-April 09

Re: Recursion help

Posted 26 April 2009 - 05:46 PM

method0910("TANGO") performs two major tasks and they are: method0910("TANG") and System.out.println("TANGO"). Here's why:

First, the input s is equal to "TANGO".
n is the length of s which is = 5
temp is a substring of s from 0 to (5-1) which is "TANG"

Therefore, method0910 eventually gets to:
1. method0910("TANG")
2. System.out.println("TANGO")

Now lets try to see the entire process visually. we call method0910("TANGO") and it does this:
1. method0910("TANG")
2. System.out.println("TANGO")


now, what does method0910("TANG") do? it calls method0910("TAN") and System.out.println("TANG"). So lets replace method0910("TANG") with method0910("TAN") and System.out.println("TANG"):

1. method0910("TAN")
2. System.out.println("TANG")
3. System.out.println("TANGO")

Now replace method0910("TAN") with method0910("TA") and System.out.println("TA"):

1. method0910("TA")
2. System.out.println("TAN")
3. System.out.println("TANG")
4. System.out.println("TANGO")

Replace method0910("TA")

1. method0910("T")
2. System.out.println("TA")
3. System.out.println("TAN")
4. System.out.println("TANG")
5. System.out.println("TANGO")

Finally, now we replace method0910("T")


1. method0910("")
2. System.out.println("T")
3. System.out.println("TA")
4. System.out.println("TAN")
5. System.out.println("TANG")
6. System.out.println("TANGO")

Notice we can't replace method0910("") anymore. This is becase the length of "" is 0, and inside the method0910, it says only to perform its two main tasks if the length n is more than 0. Thus, method0910("") does absolutely nothing. Therefore we can now we can evaluate the print statements:

T
TA
TAN
TANG
TANGO
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Recursion help

Posted 26 April 2009 - 06:23 PM

Why everybody having to wite a recursion program wrote them and then ask us to show them how it works ? :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#5 Hikaroshi  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 42
  • Joined: 26-April 09

Re: Recursion help

Posted 26 April 2009 - 07:08 PM

Thank you very much, I understand how it works now.

@pbl, this isn't my code.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Recursion help

Posted 26 April 2009 - 07:10 PM

View PostHikaroshi, on 26 Apr, 2009 - 06:08 PM, said:

Thank you very much, I understand how it works now.

@pbl, this isn't my code.

so you want us to debug code that you cut and pasted from the web ?
Was This Post Helpful? 0
  • +
  • -

#7 Hikaroshi  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 42
  • Joined: 26-April 09

Re: Recursion help

Posted 26 April 2009 - 07:17 PM

View Postpbl, on 26 Apr, 2009 - 06:10 PM, said:

View PostHikaroshi, on 26 Apr, 2009 - 06:08 PM, said:

Thank you very much, I understand how it works now.

@pbl, this isn't my code.

so you want us to debug code that you cut and pasted from the web ?

No, I just wanted to know how does the result come out to be TA TAN TANG TANGO. If I didn't come here I would never figure out why the answer is "TA TAN TANG TANGO". I know my teacher wouldn't be capable of telling me.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Recursion help

Posted 26 April 2009 - 07:20 PM

:D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1