Advertisement
Crenox

Creature & Herd Java Program

Dec 23rd, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. // Sammy Samkough
  2. // Creature & Herd
  3. // Spec: In this lab you will complete missing methods. A Creature has been almost fully implemented and a Herd is an ArrayList
  4. // of Creatures. HerdRunner is your client program.
  5.  
  6. // ArrayList of User-defined Classes
  7. public class Creature implements Comparable
  8. {
  9. private int size;
  10.  
  11. public Creature(int girth)
  12. {
  13. size = girth;
  14. }
  15.  
  16. public void setSize(int girth)
  17. {
  18. size = girth;
  19. }
  20.  
  21. /**
  22. * method isBig should return true if size > 75
  23. * method isBig should return false if size <= 75
  24. */
  25. public boolean isBig()
  26. {
  27. if (size > 75)
  28. {
  29. return true;
  30. }
  31. else
  32. {
  33. return false;
  34. }
  35. }
  36.  
  37. public boolean equals(Object obj)
  38. {
  39. Creature other = (Creature)obj;
  40.  
  41. if(size == other.size)
  42. {
  43. return true;
  44. }
  45.  
  46. return false;
  47. }
  48.  
  49. public int compareTo(Object obj)
  50. {
  51. Creature other = (Creature)obj;
  52.  
  53. if(size > other.size)
  54. {
  55. return 1;
  56. }
  57. else if(size < other.size)
  58. {
  59. return -1;
  60. }
  61.  
  62. return 0;
  63. }
  64.  
  65. public String toString()
  66. {
  67. return "" + size;
  68. }
  69. }
  70. -------------------------------------------------------------------------------------------------------------------------------
  71. // Sammy Samkough
  72. // Creature & Herd
  73. // Spec: In this lab you will complete missing methods. A Creature has been almost fully implemented and a Herd is an ArrayList
  74. // of Creatures. HerdRunner is your client program.
  75.  
  76. // ArrayList of User-defined Classes
  77. import java.util.Collections;
  78. import java.util.List;
  79. import java.util.ArrayList;
  80.  
  81. public class Herd
  82. {
  83. private List<Creature> list;
  84.  
  85. public Herd()
  86. {
  87. // point list to a new ArrayList (instantiate list)
  88. list = new ArrayList<Creature>();
  89. }
  90.  
  91. public void add(int creatureSize)
  92. {
  93. // add a new Creature to the list with the size given
  94. list.add(new Creature(creatureSize));
  95. }
  96.  
  97. /* method countBigOnes should return the count of
  98. big creatures - use the isBig() Creature method
  99. */
  100. public int countBigOnes()
  101. {
  102. int x = 0;
  103.  
  104. for (int i = 0; i < list.size(); i++)
  105. {
  106. if (list.get(i).isBig())
  107. {
  108. x++;
  109. }
  110. }
  111.  
  112. return x;
  113. }
  114.  
  115. public String toString()
  116. {
  117. return "" + list;
  118. }
  119. }
  120. -------------------------------------------------------------------------------------------------------------------------------
  121. // Sammy Samkough
  122. // Creature & Herd
  123. // Spec: In this lab you will complete missing methods. A Creature has been almost fully implemented and a Herd is an ArrayList
  124. // of Creatures. HerdRunner is your client program.
  125.  
  126. // ArrayList of User-defined Classes runner
  127. import java.util.ArrayList;
  128. import java.util.Collections;
  129.  
  130. public class HerdRunner
  131. {
  132. public static void main (String args[])
  133. {
  134. Herd bunch = new Herd();
  135.  
  136. bunch.add(33);
  137. bunch.add(115);
  138. bunch.add(16);
  139. bunch.add(83);
  140. bunch.add(45);
  141. bunch.add(96);
  142.  
  143. System.out.println(bunch);
  144. System.out.println("Big One Count = " + bunch.countBigOnes());
  145. }
  146. }
  147. /*
  148. [33, 115, 16, 83, 45, 96]
  149. Big One Count = 3
  150. Press any key to continue . . .
  151. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement