/* * Write a Critter called a sloth. * A Sloth is lazy. It always forfeits in a fight, and it eats * according to the boolean that was passed to the constructor. * It is gray and represented with an S. It usually moves North, * but every 3 moves it moves South. */ public class Sloth extends Critter { // Fields must be private. private boolean eat; private int count; // Store "magic numbers" in class constants. public static final int MOVE_INTERVAL = 3; // All classes must have a constructor. public Sloth(boolean eat) { this.eat = eat; // use "this" to refer to the field. this.count = 0; } public boolean eat() { // The following version of this method is bad: // if (eat == true) { // return true; // } else { // return false; // } // That example has bad boolean zen. Just returning "eat" // is the correct way to do it. See the book section // on boolean zen. return eat; } public Color getColor() { return Color.GRAY; } public Direction getMove() { count++; if (count % MOVE_INTERVAL == 0) { return Direction.NORTH; } else { return Direction.SOUTH; } } public String toString() { return "S"; } }