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

Rewrite towers.java (Listing 6.4) so that each tower/column will be a stack (Lis

ID: 3567413 • Letter: R

Question

Rewrite towers.java (Listing 6.4) so that each tower/column will be a stack (Listing 5.4 on p.203) and use the displayStack() method of the stack to display the content (disks on each tower/column) as shown in the sample run output below.

<<java Class>>
Link

<<Java Class>>
LinkList

<<Java Class>>
LinkedStack

<<Java Class>>
TowersApp

The UML Class diagram is shown below. You need to add a field called name to the LinkedStack class so that it can be used to identify which column/tower it is. The possible values for it will be

Explanation / Answer

    /*

     * Java Program to Solve Tower of Hanoi Problem using Stacks

     */

   

     import java.util.*;

   

     /* Class TowerOfHanoiUsingStacks */

     public class TowerOfHanoiUsingStacks

     {

         public static int N;

         /* Creating Stack array */

         public static Stack<Integer>[] tower = new Stack[4];

   

         public static void main(String[] args)

         {

             Scanner scan = new Scanner(System.in);

             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");

             int num = scan.nextInt();

             N = num;

             toh(num);

         }

         /* Function to push disks into stack */

         public static void toh(int n)

         {

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

                 tower[1].push(d);

             display();

             move(n, 1, 2, 3);       

         }

         /* Recursive Function to move disks */

         public static void move(int n, int a, int b, int c)

         {

             if (n > 0)

             {

                 move(n-1, a, c, b);   

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

                 tower[c].push(d);

                 display();                 

                 move(n-1, b, a, c);   

             }       

         }

         /* Function to display */

         public static void display()

         {

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

             System.out.println("---------------");

             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(" ");       

         }

     }

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