Question: This program creates three classes: Tower , Disk and TowerOfHanoi . You simulate a Tower with an array and the Disk s have a numeric

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. 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

***Note this requires 3 seperate classes that extends one another

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!