Advertisement
Crenox

Pet Java Program

Apr 7th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. // Sammy Samkough
  2. // The Pet class is fully coded
  3. // The class itself must be declared abstract
  4. // since it contains an abstract method
  5.  
  6. public abstract class Pet
  7. {
  8. private String myName;
  9.  
  10. public Pet(String name)
  11. {
  12. myName = name;
  13. }
  14.  
  15. public String getName()
  16. {
  17. return myName;
  18. }
  19.  
  20. public abstract String speak();
  21. }
  22.  
  23. -------------------------------------------------------------------------------------------------------------------------------
  24. // Sammy Samkough
  25. // Class Hierarchy:
  26. // ______
  27. // | |
  28. // | Pet |
  29. // |_____|
  30. // / \
  31. // / \
  32. // __/___ ___\___
  33. // | | | |
  34. // | Cat | | Dog |
  35. // |_____| |______|
  36. // |
  37. // ______|______
  38. // | |
  39. // | LoudDog |
  40. // |____________|
  41.  
  42. public class Dog extends Pet
  43. {
  44. public Dog(String name)
  45. {
  46. super(name);
  47. }
  48.  
  49. public String speak()
  50. {
  51. String str = "dog-sound";
  52.  
  53. return str;
  54. }
  55. }
  56. -------------------------------------------------------------------------------------------------------------------------------
  57. // Sammy Samkough
  58. // Part A - Write a complete class declaration for the class Cat
  59. // including implementations of its constructor and method(s)
  60. // the Cat method speak returns "meow" when it is invoked.
  61.  
  62. // Class Hierarchy:
  63. // ______
  64. // | |
  65. // | Pet |
  66. // |_____|
  67. // / \
  68. // / \
  69. // __/___ ___\___
  70. // | | | |
  71. // | Cat | | Dog |
  72. // |_____| |______|
  73. // |
  74. // ______|______
  75. // | |
  76. // | LoudDog |
  77. // |____________|
  78.  
  79. public class Cat extends Pet
  80. {
  81. public Cat(String name)
  82. {
  83. super(name);
  84. }
  85.  
  86. public String speak()
  87. {
  88. String str = "meow";
  89.  
  90. return str;
  91. }
  92. }
  93. -------------------------------------------------------------------------------------------------------------------------------
  94. // Sammy Samkough
  95. // Part B - Write a complete class declaration for the class LoudDog
  96. // including implementations of its constructor and method(s)
  97. // if the String dog-sound is returned by the Dog method speak,
  98. // then the LoudDog method speak returns a String containing
  99. // dog-sound repeated two times.
  100.  
  101. // Class Hierarchy:
  102. // ______
  103. // | |
  104. // | Pet |
  105. // |_____|
  106. // / \
  107. // / \
  108. // __/___ ___\___
  109. // | | | |
  110. // | Cat | | Dog |
  111. // |_____| |______|
  112. // |
  113. // ______|______
  114. // | |
  115. // | LoudDog |
  116. // |____________|
  117.  
  118. public class LoudDog extends Dog
  119. {
  120. public LoudDog(String name)
  121. {
  122. super(name);
  123. }
  124.  
  125. public String speak()
  126. {
  127. String str = super.speak() + super.speak();
  128.  
  129. return str;
  130. }
  131. }
  132. -------------------------------------------------------------------------------------------------------------------------------
  133. // Sammy Samkough
  134. // Pet
  135. // Spec: Create methods about Pet.
  136.  
  137. import java.util.ArrayList;
  138.  
  139. public class Kennel
  140. {
  141. private ArrayList <Pet> petList;
  142.  
  143. public Kennel()
  144. {
  145. petList = new ArrayList<Pet>();
  146. }
  147.  
  148. // Part C
  149. // Write method allSpeak. For each Pet in the kennel, allSpeak
  150. // prints a line w/ the name of the Pet followed by
  151. // the result of a call to its speak method.
  152.  
  153. public void allSpeak()
  154. {
  155. for (Pet p : petList)
  156. {
  157. System.out.println("Name: " + p.getName() + "\nVoice: " + p.speak());
  158. }
  159. }
  160.  
  161. // added this method to add Pets to petList
  162. public void addPet(Pet fred)
  163. {
  164. petList.add(fred);
  165. }
  166. }
  167. -------------------------------------------------------------------------------------------------------------------------------
  168. // Sammy Samkough
  169. // PetDriver
  170. // Spec: Print out the pets.
  171.  
  172. public class PetDriver
  173. {
  174. public static void main(String args[])
  175. {
  176. Cat mark = new Cat("Mark");
  177. Dog zach = new Dog("Zach");
  178. LoudDog harry = new LoudDog("Harry");
  179.  
  180. System.out.println("My cat named " + mark.getName() + " makes this sound: " + mark.speak());
  181. System.out.println("My dog named " + zach.getName() + " makes this sound: " + zach.speak());
  182. System.out.println("My loud dog named " + harry.getName() + " makes this sound: " + harry.speak());
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement