| ||||||||||
public abstract class Animal { public abstract String speak(); public String toString() { return "An animal";} public String favoriteFood() { return "Anything"; } public void eat() { System.out.println("Tasty!"); } } public class Dinosaur extends Animal { public static int NUM_DINOSAURS = 0; public Dinosaur(){ super(); NUM_DINOSAURS = NUM_DINOSAURS + 1; } public static boolean isExtinct() { return (NUM_DINOSAURS == 0); } public void remove() { NUM_DINOSAURS = NUM_DINOSAURS - 1; } public String speak() { return "ROAR!"; } public String favoriteFood() { return "Plants"; } }
(a) Which of the following declarations are valid? Check all that apply.
(i) Animal littleFoot = new Dinosaur();
(ii) Dinosaur littleFoot = new Dinosaur();
(iii) Animal fido = new Animal();
(iv) Dinosaur fido = new Animal();
(b) Given a Dinosaur petrie = new Dinosaur(), what do the following commands return when typed in the Dr. Java interactions pane?
(i) petrie.toString()
(ii) Dinosaur.isExtinct()
(iii) petrie.speak()
(iv) petrie.favoriteFood()
(c) Given a Animal cera = new Dinosaur(), do the following lines compile?
(i) cera.speak();
(ii) cera.remove();
(iii) cera.eat();