/ Published in: Java
This example demonstrates advanced object-oriented concepts including inheritance, getters/setters, method overloading and reflection.
Expand |
Embed | Plain Text
////////////////////////////////////////////////////////// /** * Animal.java * Represents a single Animal. */ public class Animal { private String name; private String habitat; /* constructor - Sets up the Animal with a name * @param animalName String */ { name = animalName; } /* constructor - Sets up the Animal with a name * @param animalName String */ { habitat = animalHabitat; } /* toString - Returns this Animal's name as a string * @return name String */ { return name; } } ////////////////////////////////////////////////////////// /** * Fish.java * Represents an animal of type Fish. */ import Animal; public class Fish extends Animal { private String name; /* constructor - Sets up the Animal with a name * @param animalName String */ { name = animalName; } public hasHabitat() { } public canSwim() { } /* toString - Returns this Animal's name as a string * @return name and habitat as String */ { return name + " " + habitat; } } ////////////////////////////////////////////////////////// /** * Species.java * Represents a collection of Animals of a similar * genotype and classification. */ import Animal; public class Species { private AnimalNode list; /* constructor - Sets up an initially empty list of Animals */ public Species() { list = null; } /* add - Creates new AnimalNode object, adds it to end of the linked list * @param animalName Animal the common name of the animal */ public void add(Animal animalName) { AnimalNode node = new AnimalNode(animalName); AnimalNode current; if (list == null) { list = node; } else { current = list; while (current.next != null) current = current.next; current.next = node; } } /* toString - returns this list of Animals as a string * @return result String outputs animal names line-by-line */ { AnimalNode current = list; while (current != null) { result += current.Animal.toString() + "\n"; current = current.next; } return result; } /** * AnimalNode * An inner class that represents a node in the Animal list. The * public variables are accessed by the AnimalList class. */ private class AnimalNode { public Animal Animal; public AnimalNode next; /* constructor - Sets up the node */ public AnimalNode (Animal theAnimal) { Animal = theAnimal; next = null; } } } ////////////////////////////////////////////////////////// /** * Zoo.java * Driver to exercise the AnimalList collection. There could * be many other types of collections of animals, such as a * WildernessPreserve, Aquarium, IndiginousRegion, SpeciesGroup, etc */ import AnimalList; import Animal; public class Zoo { /* * constructor * sets up a zoo with a given name * @param theZooName String name of the zoo */ zooName = theZooName; } /* * Creates an AnimalList object, adds several animals to the list, * then prints them as a representation of a Zoo. */ { AnimalList animals = new AnimalList(); //adding all my favorite animals to the Zoo animals.add (new Animal("Elephant")); animals.add (new Animal("Tiger")); animals.add (new Animal("Cheetah")); animals.add (new Animal("Lion")); animals.add (new Animal("Hawk")); animals.add (new Animal("Eagle")); animals.add (new Animal("Gorilla")); animals.add (new Animal("Chimpanzee")); animals.add (new Animal("Spider Monkey")); animals.add (new Animal("Snow Owl")); animals.add (new Animal("Otter")); animals.add (new Animal("Shark")); animals.add (new Animal("Dolphin")); animals.add (new Animal("Killer Whale")); } }
You need to login to post a comment.
