This program creates three classes: Tower , Disk and TowerOfHanoi . You simulate
ID: 669276 • Letter: T
Question
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. 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 4 disks, please display the towers to see the movement between them.
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.
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. Use the integers. The Disk sizes are numbered 1, 2, 3, …., numDisks.
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
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.
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 use a for loop and start with i=numDisks
main
void move (…)
Tower destinationTower
Get the Disk from this Tower
(array element size()-1). Remove it.
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 for the numbers
displayTowers
The TowerOfHanoi object is the main driver that plays the mathematical game. The has three static data members: an array of 3 Towers, Tower[] towers, the number of disks, int numDisks and a variable to hold the number of moves, long numMoves variable to hold the number of moves. It has three static methods: main, displayTowers(), and moveDisks(int,TowerA,TowerB,TowerC).
Table 3: TowerOfHanoi class
Data/Method TowerOfHanoi
Parameters
Description
Called From
Tower[] towers
long numMoves
int numDisks
public static void
displayTowers ()
Uses the Tower toString() method to display the towers. See the sample output below. This is only called when numDisks == 4
main
and
moveDisks
public static void
moveDisks(…)
int numberDisks, Tower beg, Tower end, Tower aux
This is the recursive method that plays the game. See the algorithm in the above text. Only call displayTowers when numDisks == 4
main
public static void
main (…)
String[] args
See algorithm below
moveDisks
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
Instantiate the three towers, calling them ‘A’, ‘B’, ‘C’
Initialize the first tower (towers[0]) with all the disks
Display the towers array, when numberDisks == 4
Call moveDisks to play the game
Display the number of moves for this number of disks
The pseudo-code for the recursive function is:
moveDisks (N, Beg, End, Aux)
{
if N=1 then
move ring (Beg->End)
increment move count // update static variable
display towers // when numDisks is 4
else
Call moveDisks (N-1, Beg, Aux, End)
Call moveDisks (1, Beg, End, Aux)
Call moveDisks (N-1, Aux, End, Beg)
endif
}
Where:
N: number of disks to move that round
Beg: This is the fromTower; the tower which has all the disks to begin with
End: This is the toTower; the tower which has all the disks at the end
Aux: This is the helpTower.
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
working tower of hanoi code
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(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.