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:
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:
To run the Hanoi applet class:
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:
To change the number of disks for which the applet solves the problem,
Now when you run the sample, the problem will be solved for the number of disks you selected.