Advertisement
advictoriam

Untitled

Feb 28th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. public class Animals
  2. {
  3.    /**
  4.       Describes the given animal.
  5.       @param pet an animal
  6.       @return a string with the animal's name, a space, and
  7.       either the trick that the animal knows (if it is a dog) or a
  8.       string "has no tricks"
  9.    */
  10.    public static String describe(Animal pet)
  11.    {
  12.       String trick =
  13.       trick = (pet instanceof Dog) ? ((Dog)pet).getTrick() : "has no tricks";
  14.       return String.format("%s %s", pet.getName(), trick);
  15.    }
  16.  
  17.    // This method is used to check your work
  18.    public static String check(String name, String trick)
  19.    {
  20.       Animal pet;
  21.       if (trick == null) pet = new Cat(name);
  22.       else pet = new Dog(name, trick);
  23.       return describe(pet);
  24.    }      
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement