Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. import java.io.File;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. import javax.sound.sampled.AudioSystem;
  6. import javax.sound.sampled.Clip;
  7.  
  8. public class ZooTester_1Kwandou {
  9.  
  10. public static void main(String[] args) {
  11. List<Animal> zoo = new ArrayList<Animal>();
  12. //Lion lion = new Lion();
  13. for(Animal animal : zoo) {
  14. System.out.println(animal);
  15. animal.speak();
  16. }
  17. /*
  18. Test out your Animal specific methods by casting several of your animals to their subclass type and then calling that specific method:
  19. Zebra zellie = (Zebra) listOfAnimals.get(2);
  20. zellie.gallup();
  21. */
  22.  
  23. }
  24. }
  25.  
  26. class Animal {
  27. // Instance variables
  28. private String name;
  29.  
  30. public Animal(String name) {
  31. this.name = name;
  32. }
  33.  
  34. public void speak() {
  35. // Nothing to see here
  36. }
  37.  
  38. public void speak(String filename) {
  39. try {
  40. Clip clip = AudioSystem.getClip();
  41. clip.open(AudioSystem.getAudioInputStream(new File(filename)));
  42. clip.start();
  43. }
  44. catch (Exception e) { e.printStackTrace(System.out); }
  45. }
  46.  
  47. public String toString() {
  48. return "Name: " + name;
  49. }
  50. }
  51.  
  52. class Lion extends Animal {
  53. // Instance variables
  54. private int age;
  55. private String gender;
  56.  
  57. public Lion() {
  58. super("N/A");
  59. }
  60.  
  61. public Lion(String name, int age, String gender) {
  62. super(name);
  63. this.age = age;
  64. this.gender = gender;
  65. }
  66.  
  67. public void speak() {
  68. super.speak("lion-roar1.wav");
  69. }
  70.  
  71. public void eat() {
  72. System.out.println(super.toString() + " ate " +
  73. (int)(Math.random()*25) + " pounds of meat!");
  74. }
  75.  
  76. public String toString() {
  77. return super.toString() + "\nAge: " + age + "\nGender: " + gender;
  78. }
  79. }
  80.  
  81. class Rabbit extends Animal {
  82. // Instance variables
  83. private int age;
  84. private String color;
  85.  
  86. public Rabbit() {
  87. super("N/A");
  88. }
  89.  
  90. public Rabbit(String name, int age, String color) {
  91. super(name);
  92. this.age = age;
  93. this.color = color;
  94. }
  95.  
  96. public void speak() {
  97.  
  98. }
  99.  
  100. public void hop() {
  101. System.out.println(super.toString() + " hopped " +
  102. (int)(Math.random()*10) + " times!");
  103. }
  104.  
  105. public String toString() {
  106. return super.toString() + "\nAge: " + age + "\nColor: " + color;
  107. }
  108. }
  109.  
  110. class Goat extends Animal {
  111. // Instance variables
  112. private int age;
  113. private int ramStrength;
  114.  
  115. public Goat() {
  116. super("N/A");
  117. }
  118.  
  119. public Goat(String name, int age, int ramStrength) {
  120. super(name);
  121. this.age = age;
  122. this.ramStrength = ramStrength;
  123. }
  124.  
  125. public void speak() {
  126.  
  127. }
  128.  
  129. public void climb() {
  130. System.out.println(super.toString() + " scaled up a " +
  131. (int)(Math.random()*100) + " foot mountain!");
  132. }
  133.  
  134. public String toString() {
  135. return super.toString() + "\nAge: " + age + "\nRam Strength: " + ramStrength;
  136. }
  137. }
  138.  
  139. class Penguin extends Animal {
  140. // Instance variables
  141. private int age;
  142. private String breed;
  143.  
  144. public Penguin() {
  145. super("N/A");
  146. }
  147.  
  148. public Penguin(String name, int age, String breed) {
  149. super(name);
  150. this.age = age;
  151. this.breed = breed;
  152. }
  153.  
  154. public void speak() {
  155.  
  156. }
  157.  
  158. public void dive() {
  159. System.out.println(super.toString() + " dove " +
  160. (int)(Math.random()*5) + " times!");
  161. }
  162.  
  163. public String getName() {
  164. return super.toString();
  165. }
  166.  
  167. public String toString() {
  168. return super.toString() + "\nAge: " + age + "\nBreed: " + breed;
  169. }
  170. }
  171.  
  172. class FallingPenguin extends Penguin {
  173. // Instance variables
  174. private int age;
  175. private String breed;
  176.  
  177. public FallingPenguin() {
  178. super();
  179. }
  180.  
  181. public FallingPenguin(String name, int age, String breed) {
  182. super(name, age, breed);
  183. }
  184.  
  185. public void speak() {
  186.  
  187. }
  188.  
  189. public void fall() {
  190. System.out.println(super.getName() + " fell!");
  191. }
  192.  
  193. public String toString() {
  194. return super.toString() + "\nAge: " + age + "\nBreed: " + breed;
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement