The Cat and Dog class below implement the Animal interface. All animals have names, but only dogs do tricks. Implement the describe method, using the instanceof operator to test whether the given animal is a dog. If so, return the name, a space, and the trick that the dog can do, such as "Helmut barks at the moon". If it is any other kind of animal, return the name, a space, and "has no tricks".

Complete the following file:

Animals.java

public class Animals { /** Describes the given animal. @param pet an animal @return a string with the animal's name, a space, and either the trick that the animal knows (if it is a dog) or a string "has no tricks" */ public static String describe(Animal pet) { // TODO: Complete this method } // This method is used to check your work public static String check(String name, String trick) { Animal pet; if (trick == null) pet = new Cat(name); else pet = new Dog(name, trick); return describe(pet); } }

Use the following files:

Animal.java

/**
   Represents an animal of some type.
*/

public interface Animal
{
   String getName();
}

Cat.java

/**
   Represents a cat.
*/

public class Cat implements Animal
{
   public Cat(String aName)
   {
      // Cats don't remember their name.     
   }
   
   public String getName()
   {
      return "Meow";
   }
}

Dog.java

/**
   Represents a dog.
*/
public class Dog implements Animal
{
   private String name;
   private String trick;

   public Dog(String aName, String aTrick)
   {
      name = aName;
      trick = aTrick;
   }
   
   public String getName() { return name; }
   
   public String getTrick() { return trick; }
}