160 likes | 305 Views
There are some details that are not as important to the concepts (classes). Object. Shape. {abstract}. interface. Rectangle. Circle. RectanglePrism. Cylinder. implements. Animal. public abstract class Animal { private int weight ;
E N D
There are some details that are not as important to the concepts (classes) ITK 179
Object Shape {abstract} interface Rectangle Circle RectanglePrism Cylinder implements ITK 179
Animal publicabstractclass Animal { privateintweight; public Animal(int lb) { // constructor; weight = lb; } publicabstract String is_a(); // What animal is this; publicabstract String eats(); // the food it eats; publicabstract String sounds(); // the sound it makes; publicint Weight() { // return the weight; returnweight; } } ITK 275
WildAnimal publicabstractclass WildAnimal extends Animal { private String fHabit; private String NativePlace=null; public WildAnimal(String feedingHabit, int weight) { super(weight); this.fHabit = feedingHabit; } public String feedingHabit() { returnfHabit;} publicvoid lives_in(String NativePlace) { this.NativePlace = NativePlace; } public String lives_in() { returnNativePlace;} } ITK 275
DomesticAnimal publicabstractclass DomesticAnimal extends Animal{ private String purpose, my_name=null; public DomesticAnimal(String purpose, int weight) { super(weight); this.purpose = purpose; } public String name() { returnmy_name; } publicvoid name(String name) { my_name = name; } public String is_for() { returnpurpose; } } ITK 275
Elephant publicclass Elephant extends WildAnimal{ public Elephant(int weight) { super("herbivorous", weight); } public String is_a() { return"elephant"; } public String eats() { return"banana"; } public String sounds() { return"enheeen"; } } ITK 275
BigCat publicabstractclass BigCat extends WildAnimal{ public BigCat(int weight) { super("carnivorous", weight); } } Tiger publicclass Tiger extends BigCat{ public Tiger(int weight) { super(weight); } public String is_a() { return"tiger"; } public String sounds() { return"Grrr! Grrr! "; } public String eats() { return"meat"; } } ITK 275
Lion publicclass Lion extends BigCat{ public Lion(int weight) { super(weight); } public String is_a() { return"lion"; } public String sounds() { return"Roarn! Roand!"; } public String eats() { return"meat"; } } ITK 275
Cow publicclass Cow extends DomesticAnimal { public Cow (int weight) { super("milk",weight); } public String is_a() { return"cow"; } public String sounds() { return"Mooo! Mooo!"; } public String eats() { return"grass"; } } ITK 275
Pet publicclass Dog extends DomesticAnimal implements Pet { private String my_address; public Dog (String name, int weight) { super("pleasure",weight); this.name(name); } public String is_a() { return"dog"; } public String sounds() { return"Woof! Woof!"; } public String likes() { return"bone"; } public String eats() { return likes(); } public String address() { returnmy_address; } public String address(String add) { returnmy_address = add; } } publicinterface Pet { public String address(); public String likes(); } Dog ITK 275
Cat publicclass Cat extends DomesticAnimal implements Pet { private String my_address; public Cat (String name, int weight) { super("pleasure",weight); this.name(name); } public String is_a() { return"cat"; } public String sounds() { return"Meow! Meow!"; } public String likes() { return"fish"; } public String eats() { return likes(); } public String address() { returnmy_address; } public String address(String add) { returnmy_address = add;} } ITK 275
publicstaticvoid aboutThisDomesticAnimal(DomesticAnimal x) { System.out.println(" Variable x is a " + x.is_a() +"."); System.out.println(x.name() + " is a " + x.Weight()+" ponds "+ x.is_a() + " for " + x.is_for() + "." ); } publicstaticvoid aboutThisWildAnimal(WildAnimal x) { System.out.println(" Variable x is a " + x.is_a() +"."); System.out.println("This " + x.is_a() + " weights " + x.Weight() + " pounds."); System.out.println("The " + x.is_a() + " is a " + x.feedingHabit()+" animal from " + x.lives_in()+"."); } ITK 275
Animal animal[]; Dog lucky; Cat mimi; Cow daisy; Tiger T; Lion L; Elephant E; animal = new Animal[6]; animal[0] = lucky = new Dog("Lucky", 25); lucky.address("Bloomington"); animal[1] = mimi = new Cat("Mimi", 15); mimi.address("Normal"); animal[2] = daisy = new Cow(500); daisy.name("Daisy"); animal[3] = T = new Tiger(300); T.lives_in("Asia"); animal[4] = L = new Lion(400); L.lives_in("Africa"); animal[5] = E = new Elephant(1200); E.lives_in("Africa"); ITK 275
for (int i=0; i<animal.length;i++) { System.out.print("\n"+i+": "); if (animal[i] instanceof DomesticAnimal) { aboutThisDomesticAnimal((DomesticAnimal) animal[i]); } if (animal[i] instanceof WildAnimal) { aboutThisWildAnimal((WildAnimal) animal[i]); } } ITK 275
Bird publicclass Cardinal extendsWildAnimalimplements Bird{ public Cardinal(int weight) { super("herbivorous", weight); } public String is_a() { return"cardinal"; } public String eats() { return"worms"; } public String sounds() { return"Gee! Gee! Gee!"; } publicString color() { return"red"; } } publicinterface Bird { String color(); } Cardinal ITK 275
publicclass Canary extendsDomesticAnimalimplements Pet, Bird { private String my_address; public Canary (String name, int weight) { super("pleasure",weight); this.name(name); } public String color() { return"yellow"; } public String is_a() { return"canary"; } public String sounds() { return"Chu! Chu! Chu!"; } public String likes() { return"seeds"; } public String eats() { return likes(); } public String address() { returnmy_address; } public String address(String add) { returnmy_address = add; } } Canary ITK 275