forked from ICSatKCC/PetObject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimalFarm.java
More file actions
83 lines (64 loc) · 2.72 KB
/
AnimalFarm.java
File metadata and controls
83 lines (64 loc) · 2.72 KB
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
/**
* ICS 211 Subclass/Superclass Example *
* Animal Farm
* This is the driver class for this project.
*
* @author Mark Nelson
*/
public class AnimalFarm {
public static void main(String[] args) {
System.out.println("Welcome to the farm");
System.out.println("At first we didn't have any animals");
Animal firstAnimal = null;
System.out.print("And then we bought a... ");
firstAnimal = new Chicken();
System.out.println(firstAnimal.getSpeciesName());
System.out.println("We liked having chickens on the farm, so we bought one more");
Chicken firstChicken = (Chicken) firstAnimal;
Chicken secondChicken = new Chicken();
System.out.print("They sound nice...");
System.out.print(firstChicken.tweet() + " ");
System.out.println(secondChicken.tweet());
System.out.println("One day, we found an egg");
Chicken anEgg = new Chicken();
System.out.println("After the first week, we checked to see if the egg had hatched = " + anEgg.isHatched());
anEgg.hatch();
System.out.println("On the third week, we checked to see if the egg had hatched = " + anEgg.isHatched());
Chicken cuteChick = anEgg;
anEgg = null;
System.out.println("About this time, our friend brought us a pet parrot");
Parrot pua = new Parrot();
System.out.println("Pets have names, so we thought... and thought...");
pua.setName("Pua");
System.out.println("...and we named it " + pua.getName() + " the " + pua.getSpeciesName());
System.out.println("We even taught " + pua.getName() + " to speak");
System.out.println(pua.tweet());
System.out.println("Next, we bought a cow");
Cow betsyTheCow = new Cow();
System.out.println("Now, we have all these animals...");
Animal[] allOfOurAnimals = new Animal[5];
allOfOurAnimals[0] = firstChicken;
allOfOurAnimals[1] = secondChicken;
allOfOurAnimals[2] = cuteChick;
allOfOurAnimals[3] = pua;
allOfOurAnimals[4] = betsyTheCow;
for ( Animal animal : allOfOurAnimals ) {
System.out.println(" " + animal.getSpeciesName());
}
System.out.println("In the morning, the birds wake us up");
Bird[] allBirds = new Bird[4];
allBirds[0] = firstChicken;
allBirds[1] = secondChicken;
allBirds[2] = cuteChick;
allBirds[3] = pua;
for( int i = 0 ; i < 9 ; i++ ) {
System.out.print( "'"
+ allBirds[i % allBirds.length].tweet()
+ "' ");
}
System.out.println("");
System.out.println("Most birds fly = " + pua.canFly());
System.out.println("But chickens don't fly very well, chickens + fly = " + cuteChick.canFly());
System.out.println("Mahalo for visiting our farm");
}
}