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

/* HanoiClient * Anderson, Franceschi */ import javax.swing.JOptionPane; import

ID: 3764141 • Letter: #

Question

/* HanoiClient
* Anderson, Franceschi
*/

import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.awt.Graphics;

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


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


// 3. Move ( numDisks - 1 ) disks from useTower to toTower
// using 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" );
}
}
}

Explanation / Answer

package hanoiclient;

import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.util.Scanner;

class TowersOfHanoi {
    int numOfDisks;
    int disk;
    int fromTower;
    int toTower;

    TowersOfHanoi(int i) {
        numOfDisks = i;
        //throw new UnsupportedOperationException("Not supported in java yet.");
      
    } // end constructor function
  
    void setDisks(int n){
        numOfDisks = n;
    }
  
   int getDisks(){
      return numOfDisks;
    }
   void updateTowers(int d, int f, int t) {
           disk = d;
            fromTower = f;
            toTower = t;
    }
} // end class toh
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 )
{
if ( numDisks == 1) {
    System.out.println("Moving from tower " + fromTower + " to tower " + toTower);
} /* end then of if */ else {
    recursiveTOfH(numDisks-1, fromTower, toTower, useTower);
    System.out.println("Moving from tower " + fromTower + " to tower " + toTower);  
    recursiveTOfH(numDisks-1, useTower, fromTower, toTower);  
} // end else
  
// ***** 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

// three variables are:
int diskNumber=1, numOfDisks; // , fromTower, toTower, numOfDisks;
// the call to moveDisks will be:
Scanner sc = new Scanner(System.in);
numOfDisks = sc.nextInt();
for (int i = 1; i<= numOfDisks; i++) {
    diskNumber = i;
    moveDisk( diskNumber, fromTower, toTower );
} // end for  

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

recursiveTOfH(numOfDisks, fromTower, toTower, useTower);
// 2. Move one disk from fromTower to toTower
    System.out.println(" Moving disk " + diskNumber + " from tower " + fromTower + " to tower " + toTower);
// Print a message to the screen, then
// call moveDisk in order to animate.


// 3. Move ( numDisks - 1 ) disks from useTower to toTower
// using 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" );
} // end while
} // end main
} // end public class HanoiClient