Question: **Below I will add source code. You have to write code wherever stub is written. AlsoYou just have to write codes on TowerofHanoi.java. The rest

**Below I will add source code. You have to write code wherever stub is written. AlsoYou just have to write codes on TowerofHanoi.java. The rest of files are for references.
TowerofHanoi.java
import java.util.Scanner;
public class TowerOfHanoi {
// Moves all of the discs from the startTower to endTower without ever stacking a
// larger disc over a smaller one.
public static void moveTower(int numberOfDiscs, StackInterface
// STUB
}
public static void main(String[] args) {
System.out.print("How many discs would you like to move? Enter an integer: ");
Scanner in = new Scanner(System.in);
int count = in.nextInt();
if (count > 40 || count
System.out.println("You can't build a stack with that height!");
System.exit(0);
}
StackInterface
StackInterface
startTower.setName("start tower");
endTower.setName("end tower");
System.out.println("Adding discs to start tower...");
for (int i = count; i > 0; i--)
startTower.push(i);
moveTower(count, startTower, endTower);
in.close();
}
}
Stackinterface.java
public interface StackInterface
Vectorstack.java
import java.util.Vector;
import java.util.EmptyStackException;
public final class VectorStack
{
private Vector
private boolean initialized = false;
private static final int DEFAULT_CAPACITY = 50;
private static final int MAX_CAPACITY = 10000;
private String name;
public VectorStack()
{
this(DEFAULT_CAPACITY);
} // end default constructor
public VectorStack(int initialCapacity)
{
checkCapacity(initialCapacity);
stack = new Vector(initialCapacity); // Size doubles as needed
initialized = true;
} // end constructor
// 6.17
public void push(T newEntry)
{
checkInitialization();
stack.add(newEntry);
} // end push
// 6.18
public T peek()
{
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
return stack.lastElement();
} // end peek
// 6.19
public T pop()
{
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
return stack.remove(stack.size() - 1);
} // end pop
// 6.20
public boolean isEmpty()
{
return stack.isEmpty();
} // end isEmpty
// 6.20
public void clear()
{
stack.clear();
} // end clear
// Throws an exception if this object is not initialized.
private void checkInitialization()
{
if (!initialized)
throw new SecurityException ("VectorStack object is not initialized properly.");
} // end checkInitialization
// Throws an exception if the client requests a capacity that is too large.
private void checkCapacity(int capacity)
{
if (capacity > MAX_CAPACITY)
throw new IllegalStateException("Attempt to create a stack " +
"whose capacity exceeds " +
"allowed maximum.");
} // end checkCapacity
// Allows the name of a stack to be set or reset
public void setName(String n) {
name = n;
}
// Gets the current name of a stack
public String getName() {
return name;
}
} // end VectorStack
Refer to the Towers of Hanoi section of the Recursion lecture slides - all of the rules and an algorithm are in the slides. Implement a Tower of Hanoi solver that uses stacks to keep track of the discs that are being moved. In the skeleton code, a moveTower method is called with only a full start stack and empty end stack. Your task is to implement the pseudocode provided in the slides, and print out some information each time you modify a tower The code currently creates a start and end stack. You will need to create a temp stack and write a recursive method that uses all three of the stacks as parameters, like the algorithm suggests. Do not modify any of the method signature lines that are provided in the skeleton code You can move a disc by popping it off the source stack and pushing it onto the destination stack. You can set your stack names with a setName method that I snuck into the interface for this lab, and retrieve the name with getName. Your output should look like the following when solving a stack of 2 How many discs would you like to move? Enter an integer: 2 Adding discs to start tower.. Moving disc 1 from start tower to temp tower. Moving disc 2 from start tower to end tower. Moving disc 1 from temp tower to end tower A stack of size 1 is easier: How many discs would you like to move? Enter an integer: 1 Adding discs to start tower. Moving disc 1 from start tower to end tower. Refer to the Towers of Hanoi section of the Recursion lecture slides - all of the rules and an algorithm are in the slides. Implement a Tower of Hanoi solver that uses stacks to keep track of the discs that are being moved. In the skeleton code, a moveTower method is called with only a full start stack and empty end stack. Your task is to implement the pseudocode provided in the slides, and print out some information each time you modify a tower The code currently creates a start and end stack. You will need to create a temp stack and write a recursive method that uses all three of the stacks as parameters, like the algorithm suggests. Do not modify any of the method signature lines that are provided in the skeleton code You can move a disc by popping it off the source stack and pushing it onto the destination stack. You can set your stack names with a setName method that I snuck into the interface for this lab, and retrieve the name with getName. Your output should look like the following when solving a stack of 2 How many discs would you like to move? Enter an integer: 2 Adding discs to start tower.. Moving disc 1 from start tower to temp tower. Moving disc 2 from start tower to end tower. Moving disc 1 from temp tower to end tower A stack of size 1 is easier: How many discs would you like to move? Enter an integer: 1 Adding discs to start tower. Moving disc 1 from start tower to end tower
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
