// getMove: center 2 times, // then WEST on the third, // then center 2 times, // then WEST on the sixth, and so on // color : red // fight : default Critter behavior (FORFEIT) // eat : default Critter behavior // toString: return a count of how many times // the sloth had an option to eat() // // Notice the absence of a fight method here -- // since the sloth is supposed to forfeit, we can // simply let inheritance give us the default from // Critter import java.awt.*; public class Sloth extends Critter { // count of how many moves have happened private int moves; // count of how many times the simulator // has asked if we want to eat: private int couldHaveEaten; // returns the Color of the Sloth -- red public Color getColor() { return Color.RED; } // 1, 2, 4, 5, 7, 8, 10, 11 CENTER // 3, 6, 9, 12, 15 WEST public Direction getMove() { moves++; if (moves % 3 == 0) { return Direction.WEST; // other times } else { // moves is not divisible by three return Direction.CENTER; // sometimes } } // returns whether this Sloth want to eat a food right now public boolean eat() { // Sloths are supposed to keep track of how many times // they could have eaten -- so increment the field couldHaveEaten++; // Sloths are supposed to do the default Critter behavior // of eating. Use super.eat() to go to our super class // and get the default behavior return super.eat(); } // returns a String representation of the Sloth public String toString() { return "" + couldHaveEaten; } }