Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially."— Presentation transcript:

1 Java: An Eventful Approach An 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

2 Topic: Recursion Where does it belong? –Before or after loops or not at all? Combine traditional and recursive Doesn’t have to be boring!

3 Traditional Recursion /** @param exponent >= 0 @returns base raised to exponent power **/ public int fastPower(int base, int exponent) { if (exponent == 0) return 1 else if (exponent%2 == 1) // exponent is odd return base * fastPower(base, exponent-1) else // exponent is even return fastPower(base * base, exponent / 2) }

4 Structural Recursion More intuitive Graphics and ActiveObjects support interesting recursive drawings.

5 Recursive Pictures Structural recursion –Contain recursive instance variables –Defined by recursive constructors –Moved with recursive methods Animate concurrent growth

6 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

7 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 broccoli private 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)

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

9 /* Draw broccoli by recursively drawing branches (and flower) */ public class Flower implements BroccoliPart { private AngLine stem; // stem of broccoli private FilledOval bud;// Flower of broccoli plant // Draw flower public Flower(Location startCoords, double size, double direction, DrawingCanvas canvas) { // Draw stem and color green stem = 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); } … }

10 Recursive data structures Natural to students –They are used to multiple instances More concrete than multiple stack frames

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


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

Similar presentations


Ads by Google