CS51 - Fall 2009 - Lecture 19

  • inheritance continued
  • assignment statements with classes and superclasses
  • discussion of book problem 17.5.1
  • Recursion
  • programming version of induction
  • 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.