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

The Towers of Hanoi puzzle has three posts and some number n of disks. Each disk

ID: 3778317 • Letter: T

Question

The Towers of Hanoi puzzle has three posts and some number n of disks. Each disk has a hole in the middle, so that it can be put on a post, and the disks are of different sizes.
Initially, the disks are all stacked on the leftmost post, with larger disks closer to the bottom and smaller disks closer to the top.

The object is move all of the disks from the leftmost post to the rightmost post.

But there are some rules that must be followed:

1. Only one disk can be held in your hand at a time. The other disks must be on posts.

2. A disk can only be put down by placing it on one of the posts. For example, you can't put a disk on the

table beside the puzzle.

3. No disk can ever be put on top of a smaller disk.

Assignment Details:
We now model the Towers of Hanoi using multiple classes and recursion. One specific object used in the Towers of Hanoi puzzle is a disk. We can start writing a class for this disk object:

Fields: An integer representing the size (width) of the disk

Methods: A get and set method for the size of the disk.

Constructor: A constructor is called when creating a new instance of the Disk (this is when you write new Disk(5) somewhere in your java program).

We use Disk to model an Object-Orient version of the Towers of Hanoi. You will have to similarly provide aclass Rod. The Rod class should consist of the following:

Fields: A collection to store the disks.

Constructor: Creates a rod with n number of disk.

Method: getDisk() and addDisk()methods to return the top disk from the rod and add a disk to top of the rod.

A method to return the contents of the rod toString() (see below).

Any additional methods that you deem necessary.

Example:123

A

The version of Hanoi we are modelling consists of 3 rods, and a number of disks, all of different sizes, which can slide onto any of these rods. The puzzle again starts with the disks in a neat stack in ascending order of size on one rod, the smallest disk positioned at the top, thus making a conical shape. The goal of this puzzle is to find the optimal solution for moving all disks from “Rod 1” to another obeying the following rules:

Only one disk may be moved at a time.

Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod.

No disk may be placed on top of a smaller disk.

For this assignment we are going to write three java classes, each with its own functionality, in different files:
in file Disk.java the class that models a disk (look at the details given above).

in file Rod.java the class that models a rod, which can hold zero or more disk objects (and keeps their order).

in file Hanoi.java the class that models the actions in the Hanoi puzzle; this class for example letsyou move a disk from one Rod to another.The Hanoi class should consist of the following:

Fields: The Hanoi class should hold the 3 rods.

Constructor: That creates a rod with n number of disk accepted from the user.

Methods: Some methods you can implement printStatus(): to print the contents of the game status after every move.

move():to move disk from one rod to another

and recursiveTOH() : this method will find the solution using recursion.You can define any additional methods you deem necessary.

Your program should start by asking the user to enter the number of disks on the source rod. For every move
show the status of each of the rod and which disk were moved from one rod to another.

Sample Output:
Enter the number of disk in Rod A: 3
123
A B C

Move Disk 1 from A to C

2
3  1
A B C

Move Disk 2 from A to B
3 2 1

A B C

Move Disk 1 from C to B

1
3 2

A B C

Move Disk 3 from A to C

1 2 3
A B C

Move Disk 1 from B to A

1 2 3
A B C

Move Disk 2 from B to C

2
1  3
A B C

Move Disk 1 from A to C

1

2

3

A B C

Explanation / Answer

import java.util.*;

/* Class TowerOfHanoi */

public class Honoi

{

     public static int N;

     /* Creating Stack array */

     //place this line in Rod.java
     public static Stack<Integer>[] tower = new Stack[4];

     public static void main(String[] args)

     {

         Scanner scan = new Scanner(System.in);

       
         //place these 3 statments in Rod.java
         tower[1] = new Stack<Integer>();

         tower[2] = new Stack<Integer>();

         tower[3] = new Stack<Integer>();
///
       
       
         /* Accepting number of disks */       

         System.out.println("Enter number of disks in Rod A:");

         int num = scan.nextInt();

         N = num;
       
         int i;
         for(i=1;i<=N;i++)System.out.print(i);
         System.out.println(" ABC");
       
       
         //Rod r = new Rod();
         toh(num);//r.toh(num);

     }

     /* Function to push disks into stack */

   
     //place this method in Rod.java and use it
     public static void toh(int n)

     {

         for (int d = n; d > 0; d--)

             tower[1].push(d);

         printSTATUS();

         move(n, 1, 2, 3);       

     }
   
     //

     /* Recursive Function to move disks */

     //place this method in Rod.java
     public static void move(int n, int a, int b, int c)

     {

         if (n > 0)

         {

             move(n-1, a, c, b);    //recursive calling

             int d = tower[a].pop();                                           

             tower[c].push(d);

            printSTATUS();                 

             move(n-1, b, a, c);     //recursive calling

         }       

     }

     /* Function to display */

     public static void printSTATUS()

     {

       

      

         for(int i = N - 1; i >= 0; i--)

         {

             String d1 = " ", d2 = " ", d3 = " ";

             try

             {

                 d1 = String.valueOf(tower[1].get(i));

             }

             catch (Exception e){

             }  

             try

             {

                 d2 = String.valueOf(tower[2].get(i));

             }

             catch(Exception e){

             }

             try

             {

                 d3 = String.valueOf(tower[3].get(i));

             }

             catch (Exception e){

             }

             System.out.println(" "+d1+"     "+d2+"     "+d3);

         }
       
         System.out.println(" A     B     C");
         System.out.println(" ");       

     }

}

ouput:-

run:
Enter number of disks in Rod A:
3
123
ABC
1          
2          
3          
A     B     C


             
2          
3           1
A     B     C


             
             
3     2     1
A     B     C


             
        1    
3     2    
A     B     C


             
        1    
        2     3
A     B     C


             
             
1     2     3
A     B     C


             
              2
1           3
A     B     C


              1
              2
              3
A     B     C


BUILD SUCCESSFUL (total time: 3 seconds)

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