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.
towers of hanoi c++ program using stacks and singly linked lists.
towers of hanoi c++ program using stacks and singly linked lists.
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner...
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner (using Stack) in C++(using data structure). Note: you have to solve for N number of disks, 3 Towers (Stacks). Do not use recursion. For better understanding play the game at least once. Link:https://www.mathsisfun.com/games/towerofhanoi.html
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...
How many moves are required to solve the Towers of Hanoi problem when we have 3...
How many moves are required to solve the Towers of Hanoi problem when we have 3 disks? Group of answer choices 5 6 8 4 7 After the following statements execute, what item is at the front of the queue? QueueInterface<String> zooDelivery = new LinkedQueue<>(); zooDelivery.enqueue("lion"); zooDelivery.enqueue("tiger"); zooDelivery.enqueue("cheetah"); String next = zooDelivery.dequeue(); next = zooDelivery.dequeue(); zooDelivery.enqueue("jaguar"); zooDelivery.enqueue("cat"); Group of answer choices "lion" "tiger" "cheetah" "jaguar" "cat" When a recursive method does not include a base case, calling the method results...
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...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A palindromic number is an integer that is the same when the digits are reversed. For example, 121 and 625526 are palindromic, but 625 is not a palindromic number. Input: The input is in ‘palindrome.txt’. The first line of the input contains the line count m (1 ≤ m ≤ 1,000), which is the number of lines that follows the first line. Each of the...
For each problem below, write a java program to solve it, name your programs as PA2_1.java...
For each problem below, write a java program to solve it, name your programs as PA2_1.java and PA2_2.java. 1. We have a steam heating boiler whose bursting pressure is known, but we of course want to use it only at pressures well below this limit. Our engineers recommend a safety factor of 3; that is, we should never exceed a pressure that is one-third of the rated bursting pressure. Write a java program that reads in the rated bursting pressure...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT