Other 2004 FR Questions FR other years Be Prepared Home
AB-3
Part (a)
public class PredatorFish extends Fish
{
  private int daysSinceLastMeal;

  public PredatorFish(Environment env, Location loc)
  {
    super(env, loc);
    daysSinceLastMeal = 0; 1
  }

  ... 2

}
Notes:
  1. This statement is optional: 0 is the default.  Or you can put it into the declaration:
      private int daysSinceLastMeal = 0; 
  2. Do not write the whole class here...

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;

  } 2
Notes:
  1. Or:
        if (env.isValid(inFront) && !env.isEmpty(inFront))
        {
          env.remove(env.objectAt(inFront));
          return true;
        }
        else
          return false;
    But don't forget isValid then.

  2. You might be tempted to also handle daysSinceLastMeal in this method, but it is not in the method's description and you might lose points, even if your overall class works as specified.

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();
  }

Other 2004 FR Questions | Back to Contents

Copyright © 2004 by Skylight Publishing
support@skylit.com