Hanoi Sample

Objectives
To demonstrate a simple Java program that solves the classic Towers of Hanoi problem.
To demonstrate running an applet and running an application.

Time Required
10 minutes.

Before You Begin
To add sample project to your workspace:

  1. Select Quick Start from the File menu.
  2. From the left-hand list, select Feature.
  3. From the right-hand list, select Add Feature.
  4. From the dialog that opens, select IBM Java Examples.  If IBM Java Examples does not appear in the list, it means that the project has previously been added to your workspace.

The IBM Java Examples project will be added to your workspace, and the source code will be compiled.  The project includes the Hanoi sample, the IBM Java AWT samples, and several Visual Composition Editor samples.

Description
The Hanoi sample package has two executable classes:  Hanoi, which is an application with text output; and HanoiApplet, which is an applet that has animated graphical output.

Both programs demonstrate the solution to the classic Towers of Hanoi problem:  If you have three posts, one of which has a series of disks stacked on it in order of diameter, move all the disks to one of the other posts, one disk at a time, without ever stacking a larger disk on top of a smaller disk.

The application outputs text instructions to the IDE Console window, telling you which disk to move to which post, until all the disks have been moved to the target post.

The applet launches the applet viewer and shows multi-colored disks being moved from one post another until the problem is solved.

Running the Hanoi Sample
To run the Hanoi application class:

  1. Select the Hanoijexec.gif (864 bytes) class in the com.ibm.ivj.example.hanoi package.
  2. From its pop-up menu, select RunRun main.
  3. The Console window will launch.  The Standard Out pane shows the order of moving four disks (numbered according to which post stack they are on top of) to numbered posts (starting on post 0, and finishing on post 2).

To run the Hanoi applet class:

  1. Select HanoiAppletjexec.gif (864 bytes) class in the com.ibm.ivj.example.hanoi package.
  2. From its pop-up menu, select Run, In Applet Viewer.
  3. The Applet Viewer will start.  The animated disks will move from post to post to show the solution to the problem.

Modifying the Default Settings
All the source code for the Hanoi sample applet and application is provided so that you can view each class and method, and experiment with making modifications to the code.  You can always reload the original edition of the sample by replacing a new edition with a previous edition from the repository.

To change the number of disks for which the application solves the problem:

  1. Select the Hanoijexec.gif (864 bytes) class.  
  2. From its pop-up menu, select Properties.
  3. Click the Program tab.
  4. In the Command Line Arguments field, enter an integer for the number of disks (for example, 8).
  5. Click OK.

To change the number of disks for which the applet solves the problem,

  1. Select the HanoiAppletjexec.gif (864 bytes) class.  
  2. From its pop-up menu, select Properties.
  3. Click the Applet tab.
  4. In the Parameters field, change the numberofdisks parameter value.
  5. Click OK.

Now when you run the sample, the problem will be solved for the number of disks you selected.