Other 2004 FR Questions | FR other years | Be Prepared Home |
public class PredatorFish extends Fish { private int daysSinceLastMeal; public PredatorFish(Environment env, Location loc) { super(env, loc); daysSinceLastMeal = 0; 1 } ... 2 }Notes:
Part (b) // if the predatorFish is able to eat, the eaten fish is removed and // true is returned; otherwise, false is returned protected boolean eat() { Environment env = environment(); Location inFront = env.getNeighbor(location(), direction()); Fish fish = (Fish)env.objectAt(inFront); 1 if (fish != null) { fish.die(); return true; } else return false; } 2Notes:
Part (c) // acts for one step in the simulation public void act() { if (!isInEnv()) return; if (eat()) daysSinceLastMeal = 0; else daysSinceLastMeal++; if (daysSinceLastMeal >= 5) die(); else super.act(); } |
Copyright © 2004 by Skylight Publishing
support@skylit.com