CS51 - Fall 2009 - Lecture 20
recursion
idea that you can define something as a smaller version (or smaller versions) of itself, plus a little extra
examples:
Broccoli
broccoli consists of a stem plus three smaller broccoli (unless it's very small, in which case you draw a flower)
BullsEyeController
target consists of an outer ring plus a smaller target (unless it's very small, in which case you draw a solid bullseye)
steps:
describe the recursion in words . . . this should suggest the recursive and base classes, as well as the instance variables in each.
define an interface with all the methods that both the base and recursive cases must implement
define the class(es) representing the base case(s)
define the constructor(s) for the recursive class(es). The recursive calls in the constructor should create objects that are simpler than the one currently being constructed. In particular, eventually you should hit the base case.
write each method in the recursive class(es) using the assumption that the method will work correctly on the simpler objects referred to by the instance variables.
Lab 7 - Recursion:
Description: <
html
> <
pdf
>
Another example:
SimpleRecScribbler
a scribble is a line attached to a slightly smaller line
note how EmptyScribble is used
notice how a new scribble is created every time the user drags the mouse