Other 2004 FR Questions FR other years Be Prepared Home
A-2
Part (a)
public class Cat extends Pet 1
{
  public Cat(String name)
  {
    super(name); 2
  }

  public String speak()
  {
    return "meow";
  }
}
Notes:
  1. Just follow the given template for the Dog class.

  2. This constructor must call the Pet constructor and pass name to it.

Part (b)
public class LoudDog extends Dog 1
{
  public LoudDog(String name)
  {
    super(name);
  }

  public String speak()
  {
    String s = super.speak(); 2
    return s + s; 3
  }
}
Notes:
  1. Do not define new fields in this class.  The myName field and getName method are already defined in Pet.

  2. Obtain the "speak" string by calling Dog's speak.

  3. Or simply
        return super.speak() + super.speak();
    

Part (c)
  // postcondition: for each pet in the kennel, its name followed
  //                by the result of a call to its speak method
  //                has been printed, one line per Pet
  public void allSpeak()
  {
    for (int i = 0; i < petList.size(); i++)
    {
      Pet pet = (Pet)petList.get(i); 1
      System.out.println(pet.getName() + " " + pet.speak());
    }
  }
Notes:
  1. The cast to Pet is required for calling Pet's methods.

Other 2004 FR Questions | Back to Contents

Copyright © 2004 by Skylight Publishing
support@skylit.com