Implementing Recursion using an explicit Run Time Stack
-
You will solve the Towers of Hanoi problem in an iterative manner by implementing recursion using an explicit runtime stack.
-
Create a new
Java
project calledP5
and importTowersOfHanoi-starter.jar
.
Your directory should look like this:
P5/ └── src ├── Frame.java ├── Hanoi.java ├── Stack.java ├── StackException.java └── StackIF.java
Do not change the files StackIF.java
and StackException.java
. Work on Frame.java
, Stack.java
, and Hanoi.java
in that order. Test your classes step by step.
-
Frame.java
describes the behavior of the run time stack frames for Hanoi. A frame object contains four variables: n: the problem size, from: the source peg position of the pile of discs to be moved, to: the destination peg position of the pile of discs to be moved, and state: the state the Hanoi method is in (see the Stack lecture slides). Complete this class first and test it using its main method. -
Stack.java
implements theStackIF
interface. Stack objects are run time stacks of frames. Complete the code and test it using its main method.**NOTICE**: In this code, a
Stack
is implemented using anArrayList
, andpush
is implemented by adding to the **END** of theArrayList
. -
Hanoi.java
implements the iterative, explicit stack-based solution. Do not change theconstructor
,getCount
,printMove
, anditHanoi
methods. You will implementrtsHanoi
using runtime stack rts. Study the Stack slides.Initially, there is one Frame [state,n,from,to] on rts as shown in the method
itHanoi
. You will need to implementrtsHanoi
, where you will keep popping frames until rts is empty. When popping a frame:- if debug, print rts (see provided code)
- if n == 0 do nothing (stack frame has already been popped)
- else if frame in state 0:
go into state 1 by pushing a frame [1,n,from,to] on the run time stack
and call hanoi(n-1,from,via) by pushing frame [0,n-1,from,via] on the run time stack - else if in state 1
call the printMove method to print disk n move,
go into state 2 by pushing a frame [2,n,from,to] on the run time stack
and call hanoi(n-1,via,to) by pushing a frame [0,n-1,via,to] on the run time stack - else (in state 2) do nothing (stack frame has already been popped)
Using 2 disks, the following files show the sample outputs:
-
Work on your own, as always.
-
Create a jar file called P5.jar that contains all the five Java files and submit via Checkin.
-
Make sure your code runs on machines in the CSB 120 lab.
-
Read the syllabus for the late policy.
-
We will be checking programs for plagiarism, so please don’t copy from anyone else.
-
100 points for perfect submission.
-
0 points for no submission, will not compile, submitted class file, etc.
-
Preliminary Tests:
-
testCompile: Checks compilation. (0 points)
-
testFrameToString: Checks toString method of Frame class. (4 points)
-
testFrameSetSTate: Checks setState method of Frame class. (4 points)
-
testOnePush: Checks push method of Stack class. (4 points)
-
testTwoPushesOnePop: Checks push and pop methods of Stack class. (4 points)
-
testHanoi1Debug: Checks iterative Tower of Hanoi solution, with 1 disk in debug mode. (15 points)
-
testHanoi2Debug: Checks iterative Tower of Hanoi solution, with 2 disks in debug mode. (15 points)
-
-
Final Tests:
-
We will also test all the other methods in the Frame class (16 more points).
-
We will also test for other situations in the Stack class (8 more points).
-
Final tests for Hanoi will include the preliminary tests, and new ones with more disks (30 more points).
ImportantSubmit P5.jar to Checkin
-