/ Published in: Java
This example demonstrates intermediate object-oriented programming concepts including Data Structures, Linked Lists and how method overriding works. In addition, it demonstrates how to document code properly, with ample comments and proper Javadocs which can generate corresponding code API docs.
Expand |
Embed | Plain Text
////////////////////////////////////////////////////////// /** * Animal.java * Represents a single Animal. */ public class Animal { private String name; /* constructor - Sets up the Animal with a name * @param animalName String */ { name = animalName; } /* toString - Returns this Animal's name as a string * @return name String */ { return name; } } ////////////////////////////////////////////////////////// /** * AnimalList.java * Represents a collection of Animals. */ import Animal; public class AnimalList { private AnimalNode list; /* constructor - Sets up an initially empty list of Animals */ AnimalList() { 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.
