java: an eventful approach an innovative approach to teaching java in cs1 † recursion kim b....
TRANSCRIPT
Java: An Eventful ApproachAn innovative approach to teaching
Java in CS1†
Recursion
Kim B. Bruce, Andrea Danyluk & Tom Murtagh
Williams College
† Partially supported by NSF CCLI grant DUE-0088895
Topic: Recursion
• Where does it belong? – Before or after loops or not at all?
• Combine traditional and recursive
• Doesn’t have to be boring!
Traditional Recursion
/** @param exponent >= 0 @returns base raised to exponent power
**/ public int fastPower(int base, int exponent) {
if (exponent == 0) return 1else if (exponent%2 == 1) // exponent is odd return base * fastPower(base, exponent-1)else // exponent is even return fastPower(base * base, exponent / 2)
}
Structural Recursion
• More intuitive• Graphics and
ActiveObjects support interesting recursive drawings.
Recursive Pictures
• Structural recursion– Contain recursive instance variables– Defined by recursive constructors– Moved with recursive methods
• Animate concurrent growth
Broccoli parts are either branches or flowers:
public interface BroccoliPart
{
/* @param x,y amount to move broccoli */
public void move( double x, double y)
}
Use interface for parts
public class BroccoliBranch implements BroccoliPart{
// How much broccoli shrinks each call public final static double TOP_FRACT = 0.8;
private BroccoliPart left, center, right; // branches of broccoliprivate AngLine stem; // stem of broccoli
private FilledOval flower; // Flower of broccoli plant
/* Draw broccoli by recursively drawing branches (and flower) */public BroccoliBranch(Location startCoords, double size,
double direction, DrawingCanvas canvas)
/* @param x,y amount to move broccoli */public void move( double x, double y)
/* Draw broccoli by recursively drawing branches (and flower) */
public BroccoliBranch(Location startCoords, double size,
double direction, DrawingCanvas canvas) {
stem = new AngLine(startCoords,size,direction,canvas);
stem.setColor(broccoliColor);
Location destCoords = stem.getEnd(); // end of stem
if ( size > MINSIZE ) { // Big enough to keep growing
left = new BroccoliBranch (destCoords, size * TOP_FRACT,
direction + Math.PI/9.0, canvas);
center = new BroccoliBranch (destCoords, ..., direction, ...);
right = new BroccoliBranch (destCoords, ..., direction - Math.PI/9.0, ...);
} else { // draw flower when small enough
left = new Flower (destCoords, ..., direction + Math.PI/9.0, canvas);
center = new Flower (destCoords, ..., direction, canvas);
right = new Flower (destCoords, ..., direction - Math.PI/9.0, canvas);
}
}
/* Draw broccoli by recursively drawing branches (and flower) */public class Flower implements BroccoliPart {
private AngLine stem; // stem of broccoliprivate FilledOval bud; // Flower of broccoli plant
// Draw flowerpublic Flower(Location startCoords, double size,
double direction, DrawingCanvas canvas) {// Draw stem and color greenstem = new AngLine(startCoords,size,direction,canvas);stem.setColor(broccoliColor);
Location destCoords = stem.getEnd(); // end of stem
bud = new FilledOval(destCoords,BUD_SIZE,BUD_SIZE,canvas);bud.setColor(Color.yellow);
}…
}
Recursive data structures
• Natural to students
– They are used to multiple instances
• More concrete than multiple stack frames
// @param x,y amount to move broccoli
public void move( double x, double y)
{
stem.move(x,y); // move stem
left.move(x,y); // move other parts
center.move(x,y);
right.move(x,y);
}
// Similar code for move in Flower, but not recursive!
Recursive Methods, too