java: an eventful approach an innovative approach to teaching java in cs1 † recursion kim b....

11
Java: An Eventful Approach An innovative approach to teaching Java in CS1 Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College artially supported by NSF CCLI grant DUE-0088895

Upload: arleen-palmer

Post on 03-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially

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

Page 2: Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially

Topic: Recursion

• Where does it belong? – Before or after loops or not at all?

• Combine traditional and recursive

• Doesn’t have to be boring!

Page 3: Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially

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)

}

Page 4: Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially

Structural Recursion

• More intuitive• Graphics and

ActiveObjects support interesting recursive drawings.

Page 5: Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially

Recursive Pictures

• Structural recursion– Contain recursive instance variables– Defined by recursive constructors– Moved with recursive methods

• Animate concurrent growth

Page 6: Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially

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

Page 7: Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially

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)

Page 8: Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially

/* 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);

}

}

Page 9: Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially

/* 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);

}…

}

Page 10: Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially

Recursive data structures

• Natural to students

– They are used to multiple instances

• More concrete than multiple stack frames

Page 11: Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially

// @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