Question

In: Computer Science

JAVA Program a solver for the Towers of Hanoi problem presented below. Submit the Following: Hanoi.java,...

JAVA Program a solver for the Towers of Hanoi problem presented below. Submit the

Following: Hanoi.java, Driver.java (contains your main method).

Rules:

a) There are three Pillars (Pillar1, Pillar2, Pillar3).

b) There are N number of disks of increasing size (disk size is indicated by an integer).

c) At the start, all disks are stacked on one of the pillars.

d) At no point can a disk of larger size be placed above a disk of smaller size (including the start state).

e) You have to move all disks from the start pillar to a target pillar.

f) Only one disk can be moved at a time.

Sample Run:

Hanoi mySolver = new Hanoi(3,1,3);

//the first parameter is the number of disks

//the second parameter is the start pillar

//the third parameter is the target

The output should be:

My Solution is:

t0 Pillar1: 3 2 1

t0 Pillar2:

t0 Pillar3:

t1 Pillar1: 3 2

t1 Pillar2:

t1 Pillar3: 1

t2 Pillar1: 3

t2 Pillar2: 2

t2 Pillar3: 1

t3 Pillar1: 3

t3 Pillar2: 2 1

t3 Pillar3:

t4 Pillar1:

t4 Pillar2: 2 1

t4 Pillar3: 3

t5 Pillar1: 1

t5 Pillar2: 2

t5 Pillar3: 3

t6 Pillar1: 1

t6 Pillar2:

t6 Pillar3: 3 2

t7 Pillar1:

t7 Pillar2:

t7 Pillar3: 3 2 1

Solutions

Expert Solution

Find below the java code for the Towers of Hanoi problem.

it is asking for three parameters:

  1. the first parameter is the number of disks
  2. the second parameter is the start pillar
  3. the third parameter is the target

Java Code:


import java.util.Scanner;
import java.util.Stack;

class Hanoi {

        public static int N;
        private int start;
        private int end;
        private int num;
        
        /* Creating Stack array */
        public static Stack<Integer>[] tower = new Stack[4];
        /* create to variable to get the number of steps required */
        static int j = 0;
        
        public Hanoi(int num, int start, int end) {
                
                this.start = start;
                this.end = end;
                this.num = num;
        
                tower[1] = new Stack<Integer>();
                tower[2] = new Stack<Integer>();
                tower[3] = new Stack<Integer>();

                int mid = 2;
                if (start == 1 && end == 2)
                        mid = 3;
                else if (start == 2 && end == 3)
                        mid = 1;
                else if (start == 3 && end == 1)
                        mid = 2;

                else if (start == 1 && end == 3)
                        mid = 2;
                else if (start == 2 && end == 1)
                        mid = 3;
                else if (start == 3 && end == 2)
                        mid = 1;
                N = num;
                toh(num, start, mid, end);
        }

        /* Function to push disks into stack */
        public static void toh(int n, int start, int mid, int end) {
                for (int d = n; d > 0; d--)
                        tower[start].push(d);
                display();
                move(n, start, mid, end);
        }

        /* 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() {
                String D1 = " ", D2 = " ", D3 = " ";
                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) {
                        }
                        D1 += d1 + " ";
                        D2 += d2 + " ";
                        D3 += d3 + " ";

                }
                System.out.println("t" + j + " Pillar1:" + new StringBuilder(D1).reverse());
                System.out.println("t" + j + " Pillar2:" + new StringBuilder(D2).reverse());
                System.out.println("t" + j + " Pillar3:" + new StringBuilder(D3).reverse());
                System.out.println("\n");
                j++;
        }

}

public class Driver {

        public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);
                /* Accepting number of disks, start pillar and target pillar */
                
                //the first parameter is the number of disks
                System.out.print("Enter number of disks: ");
                int num = scan.nextInt();
                //the second parameter is the start pillar
                System.out.print("Enter Start pillar: ");
                int start = scan.nextInt();
                //the third parameter is the target
                System.out.print("Enter end pillar: ");
                int end = scan.nextInt();
                
                Hanoi mySolver = new Hanoi(num,start,end);
        }
}

Output:

Enter number of disks: 3
Enter Start pillar: 1
Enter end pillar: 3
t0 Pillar1: 3 2 1 
t0 Pillar2:       
t0 Pillar3:       


t1 Pillar1: 3 2   
t1 Pillar2:       
t1 Pillar3: 1     


t2 Pillar1: 3     
t2 Pillar2: 2     
t2 Pillar3: 1     


t3 Pillar1: 3     
t3 Pillar2: 2 1   
t3 Pillar3:       


t4 Pillar1:       
t4 Pillar2: 2 1   
t4 Pillar3: 3     


t5 Pillar1: 1     
t5 Pillar2: 2     
t5 Pillar3: 3     


t6 Pillar1: 1     
t6 Pillar2:       
t6 Pillar3: 3 2   


t7 Pillar1:       
t7 Pillar2:       
t7 Pillar3: 3 2 1 
 
 

Related Solutions

Program a solver for the Towers of Hanoi problem presented below. Submit the following to canvas:...
Program a solver for the Towers of Hanoi problem presented below. Submit the following to canvas: Hanoi.java, Driver.java (contains your main method). This is an individual project. Students may discuss solutions to the problem but may not share code with one another. I will discuss this project during our next lecture. Rules: a) There are three Pillars (Pillar1, Pillar2, Pillar3). b) There are N number of disks of increasing size (disk size is indicated by an integer). c) At the...
Write a java program to solve Towers of Hanoi with the condition that there are "m"...
Write a java program to solve Towers of Hanoi with the condition that there are "m" number of rods and "n" number of disks. Where m >= 3 and n >=1.
Consider the following variants of the Towers of Hanoi. For each of variant, describe an algorithm...
Consider the following variants of the Towers of Hanoi. For each of variant, describe an algorithm to solve it in as few moves as possible. Prove that your algorithm is correct. Initially, all the n disks are on peg 1, and you need to move the disks to peg 2. In all the following variants, you are not allowed to put a bigger disk on top of a smaller disk. Consider the disappearing Tower of Hanoi puzzle where the largest...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there can be any amount of disks and there are either 3,4, or 5 pegs to store the disks(# of pegs and disks is manually inputted by user), in Tower of Hanoi game, the object of the game is move all the pieces from the 1st peg to the right most peg or the opposite side. You can place disks on top of each other...
1. Write a Java program and Submit only "one .java file" calledNumbers that calls the...
1. Write a Java program and Submit only "one .java file" called Numbers that calls the following methods and displays the returned value:o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer.2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive.o Write a method called larger that accepts two double parameters and...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
Problem:  Using Solver, solve the linear program to find the optimal number of batches to make of...
Problem:  Using Solver, solve the linear program to find the optimal number of batches to make of each of the three cookies. Price per chocolate chip cookie $                      1.50 Price per sugar cookie $                      1.00 Price per snickerdoodle cookie $                      1.00 Recipes for one batch Number of cookies/batch 20 20 30 Ingredient Chocolate chip cookie recipe Sugar cookie recipe Snickdoodle recipe Butter (sticks) 2 2 2 Sugar (cups) 1 2 1 Eggs 2 3 1 Chocolate chips (cups) 1 0 0...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called  Dog that contains instance data that represent the dog's name and age.   define the Dog constructor to accept and initialize instance data.   create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age).   Include a toString...
Partial of the Solver Sensitivity Report for the LP model in Problem (3) is provided below....
Partial of the Solver Sensitivity Report for the LP model in Problem (3) is provided below. Microsoft Excel 16.0 Sensitivity Report Constraints Final Shadow Constraint Allowable Allowable Cell Name Value Price R.H. Side Increase Decrease $I$9 Component A 4000 11.33 4000 1250 4000 $I$10 Component B 2667 0 3500 1E+30 833 Answer the following questions based this report. (a) If 1,000 additional units of component A are available at a unit cost of $10, should the company take it? Why...
Can you provide solution in the Excel using Solver for the below problem ? On Monday...
Can you provide solution in the Excel using Solver for the below problem ? On Monday morning, you have $3000 in cash on hand. For the next seven days, the following cash requirements must be met: Monday, $5000; Tuesday, $6000; Wednesday, $9000; Thursday, $2000; Friday, $7000; Saturday, $2000; Sunday, $3000. At the beginning of each day, you must decide how much money (if any) to withdraw from the bank. It costs $10 to make a withdrawal of any size. You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT