-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKennel.java
More file actions
34 lines (28 loc) · 896 Bytes
/
Kennel.java
File metadata and controls
34 lines (28 loc) · 896 Bytes
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
import java.util.ArrayList;
public class Kennel {
private ArrayList<Pet> petList;
/**
* Postcondition: for each Pet in the kennel, its name followed by the result of
* a call to its speak method has been printed, one line per Pet
*/
public void allSpeak() {
for (Pet p:petList) {
System.out.println(p.getName()+" "+p.speak());
}
}
// Constructor implementation not shown to student
public Kennel() {
petList = new ArrayList<Pet>();
}
// Main method not shown to students
public static void main(String[] args) {
// main is static method but petList is an instance variable --
// cannot use instance variables from within a static method!
// create an instance of a kennel and work with that
Kennel k = new Kennel();
k.petList.add(new Dog("Ricky"));
k.petList.add(new Cat("Charlemagne"));
k.petList.add(new LoudDog("Christina"));
k.allSpeak();
}
}