package Lect31Polymorphism; import java.util.*; public class ZooArrayList { private ArrayList zoo; public ZooArrayList() { zoo = new ArrayList(); } public Animal find(String whatAnimal) { for (Iterator it = zoo.iterator(); it.hasNext();) { Animal animal = (Animal)it.next(); if (animal.getName().equals(whatAnimal)) { return animal; } } return null; } public void playSounds() { for (Iterator it = zoo.iterator(); it.hasNext();) { Animal animal = (Animal)it.next(); animal.playSound(); } } public void add(Animal theAnimal) { zoo.add(theAnimal); } public static void main(String[] args) { ZooArrayList zoo = new ZooArrayList(); // Adding animals Animal animal = new Cat("Garfield"); zoo.add(animal); zoo.add(new Dog("Lassie")); zoo.add(new Lion("King of the Jungle")); // Finding one of the animals zoo.find("Lassie").roam(); System.out.println("\nAll Sounds:"); zoo.playSounds(); } }