jeops.examples.hanoi
Class Hanoi

java.lang.Object
  |
  +--jeops.examples.hanoi.Hanoi

public class Hanoi
extends java.lang.Object

This class models an encapsulation for a solution for the Towers of Hanoi problem.

Version:
0.01 15 Mar 1998
Author:
Carlos Figueira Filho (csff@cin.ufpe.br)

Field Summary
private  int destination
          The pin the disks have to be moved to.
private  int discs
          The number of discs in this problem.
private  boolean ok
          Flag which indicates whether this problem is solved.
private  java.lang.String solution
          The problem solution, made up by a Strings in the form "Disk moved from to ".
private  int source
          The pin from where the discs come.
private  Hanoi sub1
          The first subproblem used to solve this problem.
private  Hanoi sub2
          The second subproblem used to solve this problem.
 
Constructor Summary
Hanoi(int numDiscs, int source, int destination)
          Class constructor.
 
Method Summary
 void addMove(int from, int to)
          Adds a movement to the solution.
private  void dump(int spaces)
          Prints the tree for this instance of the Hanoi problem.
 int getDestination()
          Returns the destination pin for this problem.
 int getDiscs()
          Returns the number of discs of this problem.
 int getIntermediate()
          Returns the intermediate pin form this problem.
 int getSource()
          Returns the source pin for this problem.
 Hanoi getSub1()
          Returns the first subproblem for this problem.
 Hanoi getSub2()
          Returns the second subproblem for this problem.
 boolean isOk()
          Returns the state of this problem.
 void printSolution()
          Prints the tree for this instance of the Hanoi problem.
 void setOk(boolean newValue)
          Determines whether this problem has already been solved.
 void setSub1(Hanoi sub1)
          Determines the first subproblem for this problem.
 void setSub2(Hanoi sub2)
          Determines the second subproblem for this problem.
 java.lang.String toString()
          Returns a string representation of this object.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

discs

private int discs
The number of discs in this problem.

source

private int source
The pin from where the discs come.

destination

private int destination
The pin the disks have to be moved to.

sub1

private Hanoi sub1
The first subproblem used to solve this problem.

sub2

private Hanoi sub2
The second subproblem used to solve this problem.

ok

private boolean ok
Flag which indicates whether this problem is solved.

solution

private java.lang.String solution
The problem solution, made up by a Strings in the form "Disk moved from to ".
Constructor Detail

Hanoi

public Hanoi(int numDiscs,
             int source,
             int destination)
Class constructor.
Parameters:
numDiscs - the number of the discs for this instance.
source - the source pin
destination - the destination pin
Method Detail

addMove

public void addMove(int from,
                    int to)
Adds a movement to the solution.
Parameters:
from - the pin from where the disc is moved.
to - the pin to where the disc is moved.

printSolution

public void printSolution()
Prints the tree for this instance of the Hanoi problem. Useful for debugging.

dump

private void dump(int spaces)
Prints the tree for this instance of the Hanoi problem. Useful for debugging.
Parameters:
spaces - the identation for the printed output.

getDestination

public int getDestination()
Returns the destination pin for this problem.
Returns:
the destination pin for this problem.

getDiscs

public int getDiscs()
Returns the number of discs of this problem.
Returns:
the number of discs of this problem.

getIntermediate

public int getIntermediate()
Returns the intermediate pin form this problem.
Returns:
the intermediate pin form this problem.

isOk

public boolean isOk()
Returns the state of this problem.
Returns:
true if this problem has already been solved; false otherwise.

getSource

public int getSource()
Returns the source pin for this problem.
Returns:
the source pin for this problem.

getSub1

public Hanoi getSub1()
Returns the first subproblem for this problem.
Returns:
the first subproblem for this problem.

getSub2

public Hanoi getSub2()
Returns the second subproblem for this problem.
Returns:
the second subproblem for this problem.

setOk

public void setOk(boolean newValue)
Determines whether this problem has already been solved.
Parameters:
newValue - the new value for the state of this problem.

setSub1

public void setSub1(Hanoi sub1)
Determines the first subproblem for this problem.
Parameters:
sub1 - the new value for the first subproblem of this one.

setSub2

public void setSub2(Hanoi sub2)
Determines the second subproblem for this problem.
Parameters:
sub1 - the new value for the second subproblem of this one.

toString

public java.lang.String toString()
Returns a string representation of this object. Useful for debugging.
Overrides:
toString in class java.lang.Object
Returns:
a string representation of this object.