Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java, Basic Recursion LAB 1a: Towers of Hanoi Directions [ Towers.java ] 10 poin

ID: 3752212 • Letter: J

Question

Java, Basic Recursion

LAB 1a: Towers of Hanoi Directions [ Towers.java ] 10 points.

Please write the Java solveTowers() method given in class. The method should be included inside a class named Towers and should have a public static void main() method. The main() method should be used to pass the values of the parameters needed to solveTowers(), and should end after the solveTowers() method has completed. You may use hardcoded values for the variables in main() that are passed as parameters to solveTowers(), or you may take user input for the values in main() that are passed as parameter to solveTowers().

Once you have your source code working, try running the program for values of the Towers of Hanoi game that has more than 5 disks. You should try running the program for at least five different numbers of disks. Watch carefully to determine how the recursive calls are being executed for each of the five different numbers of disks you choose.

Explanation / Answer

// Java recursive program to solve tower of hanoi puzzle

  

class GFG

{

    // Java recursive function to solve tower of hanoi puzzle

    static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)

    {

        if (n == 1)

        {

            System.out.println("Move disk 1 from rod " + from_rod + " to rod " + to_rod);

            return;

        }

        towerOfHanoi(n-1, from_rod, aux_rod, to_rod);

        System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + to_rod);

        towerOfHanoi(n-1, aux_rod, to_rod, from_rod);

    }

      

    // Driver method

    public static void main(String args[])

    {

        int n = 4; // Number of disks

        towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods

    }

}

OUTPUT:

For n disks, total 2n – 1 moves are required.

// Java recursive program to solve tower of hanoi puzzle

  

class GFG

{

    // Java recursive function to solve tower of hanoi puzzle

    static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)

    {

        if (n == 1)

        {

            System.out.println("Move disk 1 from rod " + from_rod + " to rod " + to_rod);

            return;

        }

        towerOfHanoi(n-1, from_rod, aux_rod, to_rod);

        System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + to_rod);

        towerOfHanoi(n-1, aux_rod, to_rod, from_rod);

    }

      

    // Driver method

    public static void main(String args[])

    {

        int n = 4; // Number of disks

        towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods

    }

}

OUTPUT:

   Move disk 1 from rod A to rod B   Move disk 2 from rod A to rod C   Move disk 1 from rod B to rod C   Move disk 3 from rod A to rod B   Move disk 1 from rod C to rod A   Move disk 2 from rod C to rod B   Move disk 1 from rod A to rod B   Move disk 4 from rod A to rod C   Move disk 1 from rod B to rod C   Move disk 2 from rod B to rod A   Move disk 1 from rod C to rod A   Move disk 3 from rod B to rod C   Move disk 1 from rod A to rod B   Move disk 2 from rod A to rod C   Move disk 1 from rod B to rod C  

For n disks, total 2n – 1 moves are required.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote