Pet Inheritance

Consider the given class hierarchy. Note that a Cat “is-a” Pet, a Dog “is-a” Pet and a LoudDog “is-a” Dog. The class Pet is specified as an abstract class as shown in the following declaration. Each pet has a name that is specified when it is constructed.

public abstract class Pet

{

private String myName;

public Pet(String name)

{ myName = name; }

public String getName()

{ return myName; }

public abstract String speak();

}

1) Write the complete Dog class including a one parameter constructor and any required method(s). The Dog method speak returns the string “bow-wow” when invoked.

2) Write the complete Cat class including a one parameter constructor and any required method(s).

The Cat method speak returns the string “meow” when invoked.

3) Write the complete class for LoudDog. Include a constructor and method(s). The LoudDog method speak returns a String dog-sound repeated two times.

4) Consider the partial declaration of the Kennel class below. Write the Kennel allSpeak method. For each Pet in the kennel, allSpeak prints a line with the name of the pet followed by the result of a call to its speak method.

/*** This class is not complete. ***/

import java.util.ArrayList;

public class Kennel

{

private ArrayList<Pet> petList;

public Kennel()

{

petList = new ArrayList<Pet>();

}

public void add(Pet pet)

{

petList.add(pet);

}

// postcondition: for each Pet, prints name followed

// call to its speak method, one line per Pet

public void allSpeak()

{

/** to be implemented in part (4) **/

}

}

------

/*** This TESTER Class is complete ***/

public class Tester

{

public static void main(String[] args)

{

Kennel kennel = new Kennel();

kennel.add(new Dog("Fido"));

kennel.add(new Cat("Tommy"));

kennel.add(new LoudDog("Barker"));

/** < outputs >

* Fido bow-wow

* Tommy meow

* Barker bow-wow bow-wow

*/

kennel.allSpeak();

}

}