Please follow the directions/specs exactly and include detailed comments so that
ID: 3880914 • Letter: P
Question
Please follow the directions/specs exactly and include detailed comments so that I may understand.
This program creates three classes: Tower, Disk and TowerOfHanoi. You simulate a Tower with an array and the Disks have a numeric size. The Tower is an ArrayList of Disks – that is, Tower inherits from ArrayList. You will be playing the game with different disk numbers ranging from 3 to 32 and recording the number of moves for each game.
When playing the game with 5 disks, please display the towers to see the movement between them. Do this only when playing with 5 disks.
The Disk objects are very simple. They hold an integer stating its size. They have a 1-parameter constructor, Disk(int) that initializes the size and a toString() method for displaying the Disk. Disk is an immutable class.
For instance: If the game has 6 disks, then there will be 6 Disk objects and their sizes will be 1, 2, 3, 4, 5, and 6.
The Disk objects are added to the three Tower objects, which are implemented as an ArrayList of Disks. The Tower class must inherit (extends) from ArrayList<Disk>. It has two instance variables: int numDisks and char name. The Tower class has four methods: 2-parameter constructor: Tower (char, int), init(), move (Tower), and toString().
The Tower only adds Disks to the end and takes them off the end. So, to initialize the beginning Tower, you need to add them in order from largest to smallest. The Disk sizes are numbered 1, 2, 3, …., numDisks.
for (int size = numDisks; size >= 1; size--)
{
Disk disk = new Disk (size);
add (disk);
}
Table 1: Disk class
Data/Method Disk
Parameters
Description
Called From
int size
1-param
constructor:
Disk (…)
int diskSize
Initializes the disk size
main
String toString()
Return the Disk size as a String
Tower toString()
Table 2: Tower class: Inherits from ArrayList
Data/Method Tower
Parameters
Description
Called From
char name
int numDisks
2-param
constructor:
Tower (…)
char tower,
int numDisks
Initializes the tower name and # of disks and passes the # of disks to the ArrayList constructor that takes the initial array capacity as its only parameter.
First stmt in this method should be
super (numberDisks);
Main
void init()
Initializes the tower with the Disks. Only called for Tower ‘A’.
Instantiate each of the needed disks with integers from 1 to # of disks and add them to the array starting from the highest number disk.
Hint See code above
main
void move (…)
Tower destinationTower
Get the Disk from this Tower
(array element index: size()-1). Remove it and add it to the incoming destination Tower. Remember you can use all the methods of ArrayList, since you are an ArrayList by inheritance. This is 3 lines of code.
moveDisks
String toString()
Look at the Sample Ouput to see how to display the contents of a Tower. It uses the Disk toString method for the numbers
displayTowers
The TowerOfHanoi class has the main driver that plays the mathematical game.
It has three static data members:
Tower[] towers: an array of 3 Towers
int numDisks: the number of disks
long numMoves: a variable to hold the number of moves
It has three static methods:
main()
displayTowers()
moveDisks (numDisks, towerBeg, towerAux, towerEnd,).
Table 3: TowerOfHanoi class
Data/Method TowerOfHanoi
Parameters
Description
Called From
Tower[] towers
long numMoves
int numDisks
static data members
public static void
displayTowers ()
Uses the Tower toString() method to display the towers. See the sample output below. This is only called when numDisks == 5 is true
main
&
moveDisks
public static void
moveDisks(…)
int numberDisks, Tower beg, Tower aux, Tower end
This is the recursive method that plays the game. See the algorithm in the above text. Only call displayTowers when numDisks == 5 is true
main
public static void
main (…)
String[] args
See algorithm below
system
The TowerOfHanoi main method is just a for loop to play the game with different numbers of disks ranging from 3 to 32. The algorithm given below is for the code inside the loop:
Initialize the static variables to be used and updated in moveDisk method
Instantiate the three towers, calling them ‘A’, ‘B’, ‘C’
Initialize the first tower (towers[0]) with all the disks
When numberDisks == 5 is true, display the towers array
Call moveDisks recursive method to play the game
Display the number of moves for this number of disks
.....................................................................................................................................
Sample Output
Towers of Hanoi
===============
Moving 3 completed in 7 moves!
Moving 4 completed in 15 moves!
Towers:
Tower A: [5, 4, 3, 2, 1]
Tower B: []
Tower C: []
Towers:
Tower A: [5, 4, 3, 2]
Tower B: [1]
Tower C: []
Towers:
Tower A: [5, 4, 3]
Tower B: [1]
Tower C: [2]
Towers:
Tower A: [5, 4, 3]
Tower B: []
Tower C: [2, 1]
Towers:
Tower A: [5, 4]
Tower B: [3]
Tower C: [2, 1]
Towers:
Tower A: [5, 4, 1]
Tower B: [3]
Tower C: [2]
Towers:
Tower A: [5, 4, 1]
Tower B: [3, 2]
Tower C: []
Towers:
Tower A: [5, 4]
Tower B: [3, 2, 1]
Tower C: []
Towers:
Tower A: [5]
Tower B: [3, 2, 1]
Tower C: [4]
Towers:
Tower A: [5]
Tower B: [3, 2]
Tower C: [4, 1]
Towers:
Tower A: [5, 2]
Tower B: [3]
Tower C: [4, 1]
Towers:
Tower A: [5, 2, 1]
Tower B: [3]
Tower C: [4]
Towers:
Tower A: [5, 2, 1]
Tower B: []
Tower C: [4, 3]
Towers:
Tower A: [5, 2]
Tower B: [1]
Tower C: [4, 3]
Towers:
Tower A: [5]
Tower B: [1]
Tower C: [4, 3, 2]
Towers:
Tower A: [5]
Tower B: []
Tower C: [4, 3, 2, 1]
Towers:
Tower A: []
Tower B: [5]
Tower C: [4, 3, 2, 1]
Towers:
Tower A: [1]
Tower B: [5]
Tower C: [4, 3, 2]
Towers:
Tower A: [1]
Tower B: [5, 2]
Tower C: [4, 3]
Towers:
Tower A: []
Tower B: [5, 2, 1]
Tower C: [4, 3]
Towers:
Tower A: [3]
Tower B: [5, 2, 1]
Tower C: [4]
Towers:
Tower A: [3]
Tower B: [5, 2]
Tower C: [4, 1]
Towers:
Tower A: [3, 2]
Tower B: [5]
Tower C: [4, 1]
Towers:
Tower A: [3, 2, 1]
Tower B: [5]
Tower C: [4]
Towers:
Tower A: [3, 2, 1]
Tower B: [5, 4]
Tower C: []
Towers:
Tower A: [3, 2]
Tower B: [5, 4, 1]
Tower C: []
Towers:
Tower A: [3]
Tower B: [5, 4, 1]
Tower C: [2]
Towers:
Tower A: [3]
Tower B: [5, 4]
Tower C: [2, 1]
Towers:
Tower A: []
Tower B: [5, 4, 3]
Tower C: [2, 1]
Towers:
Tower A: [1]
Tower B: [5, 4, 3]
Tower C: [2]
Towers:
Tower A: [1]
Tower B: [5, 4, 3, 2]
Tower C: []
Towers:
Tower A: []
Tower B: [5, 4, 3, 2, 1]
Tower C: []
Moving 5 completed in 31 moves!
Moving 6 completed in 63 moves!
…
Moving 30 completed in 1073741823 moves!
Moving 31 completed in 2147483647 moves!
Moving 32 completed in 4294967295 moves!
Data/Method Disk
Parameters
Description
Called From
int size
1-param
constructor:
Disk (…)
int diskSize
Initializes the disk size
main
String toString()
Return the Disk size as a String
Tower toString()
Explanation / Answer
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(" ");
}
}