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:
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(); } } }