Posted By

bcmoney on 08/17/12


Tagged

linkedlist datastructures


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

gnuskool


Java data structures example


 / 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.

  1. //////////////////////////////////////////////////////////
  2. /**
  3.  * Animal.java
  4.  * Represents a single Animal.
  5.  */
  6. public class Animal
  7. {
  8. private String name;
  9.  
  10. /* constructor - Sets up the Animal with a name
  11.   * @param animalName String
  12.   */
  13. public Animal(String animalName)
  14. {
  15. name = animalName;
  16. }
  17.  
  18.  
  19. /* toString - Returns this Animal's name as a string
  20.   * @return name String
  21.   */
  22. public String toString()
  23. {
  24. return name;
  25. }
  26. }
  27.  
  28.  
  29.  
  30. //////////////////////////////////////////////////////////
  31. /**
  32.  * AnimalList.java
  33.  * Represents a collection of Animals.
  34.  */
  35. import Animal;
  36.  
  37. public class AnimalList
  38. {
  39. private AnimalNode list;
  40.  
  41. /* constructor - Sets up an initially empty list of Animals */
  42. AnimalList()
  43. {
  44. list = null;
  45. }
  46.  
  47. /* add - Creates new AnimalNode object, adds it to end of the linked list
  48.   * @param animalName Animal the common name of the animal
  49.   */
  50. public void add(Animal animalName)
  51. {
  52. AnimalNode node = new AnimalNode(animalName);
  53. AnimalNode current;
  54.  
  55. if (list == null)
  56. {
  57. list = node;
  58. }
  59. else
  60. {
  61. current = list;
  62. while (current.next != null)
  63. current = current.next;
  64. current.next = node;
  65. }
  66. }
  67.  
  68. /* toString - returns this list of Animals as a string
  69.   * @return result String outputs animal names line-by-line
  70.   */
  71. public String toString ()
  72. {
  73. String result = "";
  74.  
  75. AnimalNode current = list;
  76.  
  77. while (current != null)
  78. {
  79. result += current.Animal.toString() + "\n";
  80. current = current.next;
  81. }
  82.  
  83. return result;
  84. }
  85.  
  86. /**
  87.   * AnimalNode
  88.   * An inner class that represents a node in the Animal list. The
  89.   * public variables are accessed by the AnimalList class.
  90.   */
  91. private class AnimalNode
  92. {
  93. public Animal Animal;
  94. public AnimalNode next;
  95.  
  96. /* constructor - Sets up the node */
  97. public AnimalNode (Animal theAnimal)
  98. {
  99. Animal = theAnimal;
  100. next = null;
  101. }
  102. }
  103. }
  104.  
  105.  
  106. //////////////////////////////////////////////////////////
  107. /**
  108.  * Zoo.java
  109.  * Driver to exercise the AnimalList collection. There could
  110.  * be many other types of collections of animals, such as a
  111.  * WildernessPreserve, Aquarium, IndiginousRegion, SpeciesGroup, etc
  112.  */
  113. import AnimalList;
  114. import Animal;
  115.  
  116. public class Zoo
  117. {
  118. String zooName = "JAVA"; //name of zoo with default (displayed in output)
  119.  
  120. /*
  121.   * constructor
  122.   * sets up a zoo with a given name
  123.   * @param theZooName String name of the zoo
  124.   */
  125. public Zoo(String theZooName) {
  126. zooName = theZooName;
  127. }
  128.  
  129. /*
  130.   * Creates an AnimalList object, adds several animals to the list,
  131.   * then prints them as a representation of a Zoo.
  132.   */
  133. public static void main (String[] args)
  134. {
  135. AnimalList animals = new AnimalList();
  136.  
  137. //adding all my favorite animals to the Zoo
  138. animals.add (new Animal("Elephant"));
  139. animals.add (new Animal("Tiger"));
  140. animals.add (new Animal("Cheetah"));
  141. animals.add (new Animal("Lion"));
  142. animals.add (new Animal("Hawk"));
  143. animals.add (new Animal("Eagle"));
  144. animals.add (new Animal("Gorilla"));
  145. animals.add (new Animal("Chimpanzee"));
  146. animals.add (new Animal("Spider Monkey"));
  147. animals.add (new Animal("Snow Owl"));
  148. animals.add (new Animal("Otter"));
  149. animals.add (new Animal("Shark"));
  150. animals.add (new Animal("Dolphin"));
  151. animals.add (new Animal("Killer Whale"));
  152.  
  153. System.out.print("'" + zooName + "' Zoo. ");
  154. System.out.println("Proud home to the following animals:");
  155. System.out.println(animals);
  156. }
  157. }

Report this snippet  

You need to login to post a comment.