CS 051 | Fall 2011 |
If we consider a drawing of a target, we might view it is a collection of concentric rings,
with a bull's eye at the center. We could imagine writing a program to draw such an object
using a while loop.
There is another way. We could define a Target recursively. For example, we could define a
Target to be an outer ring combined with a smaller Target inside. If we do this, then the
smaller Target inside would be defined as its outer ring, combined with an even smaller
Target. In order to stop this process so that it doesn't continue forever, we can decide that
if the smaller inside Target is below a certain size, we will just draw a bull's eye, and
stop creating smaller Targets.
In this example, creating a Target from an outer ring and a smaller Target is called our
recursive case. Just drawing the bull's eye is called our base case.
Our class example BullsEyeController shows what this defnition would look like in Java code.
We follow four basic steps when we think about recursion:
We also looked at writing a recursive version of our Scribbler program from earlier in the
semester. This program looked very similar to our bull's eye program, with a couple of
interesting differences.
The Scribbler recursive class constructor does not actually
recursively call itself. The recursive class does, however, declare an instance variable
of the same type that it is implemented by the class, which indicates recursion.
Also, the Scribbler program uses an empty base case. The base case does not necessarily need
to contain an object, but it needs to represent a simpler object of the same type. In this
example, and empty scribble is still a scribble.
The class example
SimpleRecScribbler