By the completion of the lab, we will have a program that will generate the following output:
I am a cow and I go moo I eat hay I am a cat and I go meow I eat mice I do tricks. I can hang from the drapes.
| Animal |
| # kind: String |
| + Animal()
+ toString(): String + abstract speak():String + static main(args: String[]) |
Modify the main() method in the Animal class so that it no longer refers to the Pig class, which we won't be using in this lab. Then compile and run the Animal class using:
javac Animal.java
java Animal
Following the example of the Cat and Cow classes, define your own subclass of Animal. Implement its speak() method in a way that is appropriate for that animal.
Modify the Animal class by defining an abstract eat() method. This method has no parameters and returns a String(). Then modify the Cat and Cow subclasses and the subclass that you created so that all three classes implement the eat() method. When you're done with this step, your program should generate something like the following output:
I am a cow and I go moo I eat hay I am a cat and I go meow I eat mice I am a dog and I go woof I eat carpets
The Trainable interface has the following design:
| <<interface>>
Trainable |
| + abstract doATrick():String |
Provide an implementation of the Trainable interface for both the Cat class and the class you defined. To implement an interface you must modify the class definition and then provide an implementation of all the methods defined in the interfaace. In this case you would modify the Cat definition to:
public class Cat extends Animal implements Trainable
You would then provide a definition of the doATrick() method in the Cat class. This is similar to the way you defined an implementation of the actionPerformed() method when you implemented the ActionListener interface in the GUI files of previous labs.
To test your code, modify the main() program to include the following lines:
Trainable pet = new Cat();
System.out.println(pet.doATrick());
Modify main() so that it also calls doATrick() on the Animal subclass that you created.
Given the classes and interfaces you have defined, which of the following would be valid. Explain. If you don't know the answers, experiment by typing the code into your program and testing it.
If you've finished the other parts of this lab and would enjoy an additional challenge, try this problem: Define a new interface named Predator, which contains the preysOn() method. This method takes a single parameter of type Animal and returns a boolean. For a class that implements this interface, the preysOn() interface would define all the animals that that animal preys on.
To test this method define an Animal subclass such as Wolf that preys on Cats and other small animals. One way to implement preysOn() would use the built-in instanceof operator. This is a boolean operator that has the form obj instance class. For example, if you have created a String instance named str, then str instanceof String would be true.
Before handing in your programs, be sure to document your source code. Every method must have a comment block that describes the method, its parameters, and its return value.
Have your work checked by the laboratory instructor or TA. Print out and hand in a copy of the Animal.java, Cat.java files and the source code for the class you created.