# Help in understanding this recursive method

• November 20th, 2012, 11:40 AM
jameschristopher06
Help in understanding this recursive method
We are learning recursion in my intro to Java class, and I am having a hard time understanding how the method in the example given works. Any explanation as to what is happening when the method is called would be appreciated.

Here is the code:

Code :

```public class Hanoi   private int n; private int pegA; private int pegB;   public Hanoi(int in_n, int in_pegA, int in_pegB) { n = in_n; pegA = in_pegA; pegB = in_pegB; }   public void makemoves() { if (n==1) System.out.format("%d ==> %d%n", pegA, pegB) else { int otherPeg = 6 - pegA - pegB; // 1 + 2 + 3 =6 Hanoi firstmove = new Hanoi (n-1, pegA, otherPeg); firstmove.makemoves(); System.out.format("%d ==> %d%n", pegA, pegB); Hanoi secondmove = new Hanoi (n-1, otherPeg, pegB); secondmove.makemoves(); } } }```
• November 20th, 2012, 11:51 AM
curmudgeon
Re: Help in understanding this recursive method
Cross-posted here. Most appreciate it if when you cross-post a question, you provide links to the cross-post so we can avoid duplicating effort that's already been exerted. We're volunteers and value our free time as much as you do.
• November 20th, 2012, 12:23 PM
jameschristopher06
Re: Help in understanding this recursive method
@curmudgeon

Will do.

~~~~

So what order would the code execute in.

Obviously it would first run

Code :

```Hanoi firstmove = new Hanoi (n-1, pegA, otherPeg); firstmove.makemoves();```

but then would it run Hanoi firstmove = new Hanoi (n-1, pegA, otherPeg) again, or would it run System.out.print?