-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZooTester_1Perrott.java
184 lines (147 loc) · 4.15 KB
/
ZooTester_1Perrott.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Name: Samuel Perrott
// Assignment: Inherits-A5: Zoo Lab
import java.util.List;
import java.util.ArrayList;
public class ZooTester_1Perrott {
public static void main(String[] args) {
List<Animal> animals = new ArrayList<>();
animals.add(new Animal("Joe"));
animals.add(new Zebra());
animals.add(new Zebra("Zellie", 20));
animals.add(new Dolphin("Dora", 120.5));
animals.add(new AtlanticSpotted("Marianne", 100.5, 99));
animals.add(new Panda("Po", 515.5));
animals.add(new Lion("Simba", 381));
for (Animal a : animals) {
System.out.println(a);
a.speak();
}
System.out.println();
Zebra zellie = (Zebra)animals.get(2);
zellie.gallop();
System.out.println();
Dolphin dora = (Dolphin)animals.get(3);
dora.blowhole();
System.out.println();
AtlanticSpotted marianne = (AtlanticSpotted)animals.get(4);
marianne.blowhole();
marianne.bubbles();
System.out.println();
Panda po = (Panda)animals.get(5);
po.eatBamboo();
System.out.println();
Lion simba = (Lion)animals.get(6);
simba.pounce();
}
}
class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public void speak() {}
public String toString() {
return name;
}
}
class Zebra extends Animal {
private int stripesCount;
public Zebra() {
super(null);
}
public Zebra(String name, int stripesCount) {
super(name);
this.stripesCount = stripesCount;
}
@Override
public String toString() {
return "This zebra's name is " + super.toString() + " and has " + stripesCount + " stripes.";
}
public void gallop() {
System.out.println("The zebra " + super.toString() + " galloped.");
}
@Override
public void speak() {
System.out.println("Neigh");
}
}
class Dolphin extends Animal {
private double length;
public Dolphin() {
super(null);
}
public Dolphin(String name, double length) {
super(name);
this.length = length;
}
public void blowhole() {
System.out.println(super.toString() + " just spewed water!");
}
@Override
public void speak() {
System.out.println("Chirp");
}
@Override
public String toString() {
return "This dolphin's name is " + super.toString() + " and is " + length + " inches long.";
}
}
class AtlanticSpotted extends Dolphin {
private int spotsCount;
public AtlanticSpotted() {
super();
}
public AtlanticSpotted(String name, double length, int spotsCount) {
super(name, length);
this.spotsCount = spotsCount;
}
@Override
public String toString() {
return super.toString() + " Also, the dolphin has " + spotsCount + " spots.";
}
public void bubbles() {
System.out.println("Bubbles appeared.");
}
}
class Panda extends Animal {
private double pounds;
public Panda() {
super(null);
}
public Panda(String name, double pounds) {
super(name);
this.pounds = pounds;
}
@Override
public void speak() {
System.out.println("Burp");
}
public void eatBamboo() {
System.out.println("Yummy");
}
@Override
public String toString() {
return "This panda's name is " + super.toString() + " and weighs " + pounds + " pounds.";
}
}
class Lion extends Animal {
private int hairsCount;
public Lion() {
super(null);
}
public Lion(String name, int hairsCount) {
super(name);
this.hairsCount = hairsCount;
}
public void pounce() {
System.out.println(super.toString() + " pounced on you.");
}
@Override
public void speak() {
System.out.println("Rrooaaarrr!!");
}
@Override
public String toString() {
return "This lion's name is " + super.toString() + " and has " + hairsCount + " hairs.";
}
}