CS111 Lab 6
Thinking about recursion problems
- Understand the pattern.
Draw out the first couple of cases involving the
smallest number of recursive calls. You should understand how to create
the pattern given any arguments for the pattern.
- Identify a single recursion layer.
What distinguishes recursion_method(level) from
recursion_method(level-1)? What is added on with
each additional level of recursion?
- Figure out how to move to the next recursion layer.
Once we have executed a single recursion layer, we need to move into
position to execute the next recursion layer. What is this pattern?
It should be the same no matter which two recursion layers we are
moving between.
- What is the next recursion layer call?
Generally, the arguments will have to be modified for the next level
(or else we'd create an endless loop)
- How do we meet our invariant, if any?
In order for recursive methods to work, they must meet strictly
defined behaviours. Often, this means that objects must be placed
in their starting positions before exiting the method
(for drawing recursive picture patterns).
- What is the base case?
Endless loops are bad. When does the recursive method stop?
Programming recursion problems
- Follow all the steps in Thinking about recursion problems (above),
first.
- Write all code on paper first. Try it out line by line using our brain
and pencil and paper with the simplest test cases to make sure it works.
- Finally, type the code into the computer and test it out.
- SAVE files before every compilation.
It is very easy to accidentally create an endless loop when solving
recursion problems. If we try to run our program and it has an endless
loop, we will hang the computer and perhaps be forced to restart.
We can tell when this happens because we won't be able to get control
of the mouse again. In these cases, first try to force quit the program
(apple-option-esc). If that doesn't work, restart the computer
(ctrl-apple-powerKey).
- Use System.out.println in all our methods to help with debugging.
Lab programming problems
The programming problems for this lab all take place in extensions
of TurtleWorld. The code is in the CS111Lab6 folder within the
/usr/users/cs111/download/labs_morning directory on
nike.wellesley.edu. The problems are arranged in order of difficulty
with the easiest problem first. Understanding problem 4 will help with
the second problem on the homework. Understanding problem 5 will help
with the third problem on the homework. For each world below, we will be
modifying the WorldName.java file and testing it by loading the
WorldName.html file into AppletViewer.
- BoomerangWorld
- EiffelWorld
- InvertedTrianglesWorld
- NestedWindowWorld
- NestedPolygonWorld
Working samples of the above worlds are available in the Test folder
which is included in the CS111Lab6 folder.