// CSE 142, Autumn 2009, Marty Stepp // A Toad is a critter very similar to a 5-year old Frog, except that he // hops west instead of east. // We implement this by using inheritance and overriding just selected parts // of the Frog's movement behavior (specifically, his getHop method). import java.awt.*; public class Frog extends Critter { private int age; private int count; public Frog(int age) { this.age = age; this.count = 0; } // New method. Returns the direction the frog should hop. // This method was added just so that subclasses could potentially // override it if they want to hop in a different direction. public Direction getHop() { return Direction.EAST; } public Color getColor() { return Color.GREEN; } // Modified. Now calls getHop instead of just returning EAST. // This allows a subclass (e.g. Toad) to change the hop direction // by overriding getHop if so desired. public Direction getMove() { count++; if (count >= age) { // go EAST once every 'age' moves count = 0; return getHop(); } else { return Direction.CENTER; } } public String toString() { return "F"; } }