Java Reference
In-Depth Information
public
Trackable cloneTarget() {
Tower clone
#
new
Tower();
clone.restore(
this
);
return
clone;
}
public
String toString() {
String result
#
"";
for
(
int
i
#
0; i < 10; i
!!
)
result
!#
queue[i]
!
" ";
return
result;
}
}
Class
Hanoi
represents a complex recoverable object, i.e. the container of
the three
Tower
objects. Method
move()
causes a state transition and thus its
invocations must be registered.
public class
Hanoi
implements
Trackable {
Tower[] towers
#
new
Tower[3];
DrawingArea drawingArea
#
null
;
public void
init() {
try
{
towers[0]
#
new
Tower();
towers[1]
#
new
Tower();
towers[2]
#
new
Tower();
towers[0].init(10);
// registers the three Tower objects with three
// different labels
Tracker.bind(towers[0], "tower_1");
Tracker.bind(towers[1], "tower_2");
Tracker.bind(towers[2], "tower_3");
}
catch
(Exception e) { e.printStackTrace(); }
}
// moves a disc between two pegs
public void
move(String from, String to) {
int
f
#
towers[Integer.parseInt(from)].sizeOfFirst();
int
t
#
towers[Integer.parseInt(to)].sizeOfFirst();
if
(f >
#
t && t !
#
0)
return
;
// records the state of this object before
// the execution of method move()
Tracker.record(
this
, "move", Tracker.BEFORE);
towers[Integer.parseInt(to)].push(
towers[Integer.parseInt(from)].pull() );
drawingArea.drawingPanel.repaint();
// records the state of this object after
// the execution of method move()
Tracker.record(
this
, "move", Tracker.AFTER);
}