Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I will reward anyone 5 starts if you give it a good attempt. Willing to create s

ID: 3555510 • Letter: I

Question

I will reward anyone 5 starts if you give it a good attempt. Willing to create second "fake question" for you to answer and get 1500 extra points as well.

In thid dropbox link, you will find the source files needed to complete this question. https://www.dropbox.com/sh/276hl2ekfq9jjhq/iYf89gbIlA

Open the HanoiClient.java source file. Searching for five asterisks (*****) in the source code will position you to the code section where you will add your code. In this task, you will fill in the code inside the recursiveTOfHmethod to solve the Towers of Hanoi problem.

Example13.9

The framework will animate your code so that you get some feedback on the correctness of your code. It will display the disks being moved from one tower to another until the whole set of disks has been moved from the left tower to the right tower. Code to enforce the rules has already been written.

To test your code, compile and run the application; when the program begins, a dialog box will prompt you for the number of disks.

Click "Cancel" to exit the program; click "OK" to continue and animate you code.

If you enter an integer less than 1 or greater than 9, the program will use a dfault value of 4.

X In the file HanoiClient.java, the recursiveTOfH method header is:

public void recursiveTOfH(int numDisks, int fromTower,

int toTower, int useTower)

This method takes four parameters: numDisks, representing the number of disks to be moved, and 3 ints representing the tower to move the disks from, the tower to move the disks to, and the tower to use to accomplish that task of moving numDisks disks from tower fromTower to tower toTower. For instanace, with five disks, our method call in the main method is:

recursiveTOfH(5, 0, 2, 1);

The preceding method call is interpreted as: move 5 disks from tower 0 to tower 2, using tower 1 as a temporary holding tower.

X Your code goes in thre places, all of them inside the if statement.

1. First, you need to move all the disks except the bottom one from fromTower (source tower, "left tower" on the figures) to the useTower (temporary tower, "middle tower" on the figures) using the toTower ("destination tower, right tower" on the figures). You do this by calling recursiveTOfH with the appropriate arguments.

2. Then, you need to move the bottom disk from the fromTower (source tower, "left tower" on the figures) to the toTower(destination tower, "right tower" on the figures). To track your progress, output the move to the command line ("Move disk x from tower y to tower z"). You also need to call the moveDisk method so that the code animates. The API of moveDisk is explained in Example 13.9

3. Finally, you need to move all the disks from the useTower (temporary tower, "middle tower" on the figures) to the toTower (destination tower, "right tower" on the figures). Again, you call recursiveTOfH.

For example, if you run your program with three disks, and assuming the towers are labeled 0, 1, and 3 from left to right, the command line output of your method should read something like:

Move disk 1 from tower 0 to tower 2

Move disk 2 from tower 0 to tower 1

Move disk 1 from tower 2 to tower 1

etc...

Explanation / Answer

import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class HanoiClient extends JFrame
{
private TowersOfHanoi tOfH;
boolean started = false;

public HanoiClient( )
{
tOfH = new TowersOfHanoi( 4 );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 500, 300 );
setVisible( true );
}

public TowersOfHanoi getTOfH( )
{
return tOfH;
}

public void setStarted( boolean b )
{
started = b;
}

public void recursiveTOfH( int numDisks, int fromTower, int toTower, int useTower )
{
// ***** Student writes the body of this method *****
//
// Using recursion, transfer numDisks disks from the tower
// fromTower to the tower toTower using the tower
// useTower

// The disks are numbered as follows: if we started with n disks,
// the disk at the top is disk # 1
// and the disk at the bottom is disk # n

// We call the moveDisk method inside the body of this method

// The moveDisk method moves one disk and takes 3 arguments:
// an int, representing the disk number to be moved
// an int, representing the tower to move the disk from
// an int, representing the tower to move the disk to

// So if these three variables are:
// diskNumber, fromTower, and toTower
// then the call to moveDisks will be:

// moveDisk( diskNumber, fromTower, toTower );

if ( numDisks > 0 )
{
// Student code starts here
// 1. Move ( numDisks - 1 ) disks from fromTower
// to useTower using toTower
   recursiveTOfH(numDisks-1,fromTower,useTower,toTower);

// 2. Move one disk from fromTower to toTower
// Print a message to the screen, then
// call moveDisk in order to animate.
   moveDisk(numDisks,fromTower,toTower);
     

     
// 3. Move ( numDisks - 1 ) disks from useTower to toTower
// using fromTower
   recursiveTOfH(numDisks-1,useTower,toTower,fromTower);

}

// Base case: 0 disks to move ==> do nothing

//
// end of student code
//
}

public void moveDisk( int diskNumber, int fromTower, int toTower )
{
repaint( );
try
{
Thread.sleep( 1000 ); // wait for the animation to finish
}
catch ( Exception e )
{
}
// update parameters
tOfH.updateTowers( diskNumber, fromTower, toTower );
}

public void paint( Graphics g )
{
if ( started )
{
super.paint( g );
tOfH.draw( g );
}
}

public int getNumberOfDisks( )
{
boolean goodInput = false;
int numberDisks = 4; // will be reassigned - default is 4
while ( !goodInput )
{
try
{
String answer = JOptionPane.showInputDialog( null, "Enter number of disks between 1 and 9" );
if ( answer != null )
{
numberDisks = Integer.parseInt( answer );
goodInput = true;
}
else
{
System.exit( 0 );
}
}
catch ( Exception e )
{}
}
return numberDisks;
}

public static void main( String [] args )
{
HanoiClient app = new HanoiClient( );
// ask user for number of disks
while ( true )
{
int numDisks = app.getNumberOfDisks( );
( app.getTOfH( ) ).setDisks( numDisks );
app.setStarted( true );
// start
app.recursiveTOfH( ( app.getTOfH( ) ).getDisks( ), 0, 2, 1 );
// finish last step in animation
app.repaint( );
System.out.println( "Done " );
// done

try
{
Thread.sleep( 5000 ); // wait for the animation to finish
}
catch ( Exception e )
{
}
JOptionPane.showMessageDialog( null, "Done" );
}
}
}