Animal - PowerPoint PPT Presentation

animal n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Animal PowerPoint Presentation
play fullscreen
1 / 14
Animal
125 Views
Download Presentation
cuyler
Download Presentation

Animal

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 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 179

  2. 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 179

  3. 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 179

  4. 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 179

  5. 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 179

  6. 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 179

  7. 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 179

  8. 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 179

  9. 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 179

  10. 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 179

  11. 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 179

  12. 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 179

  13. 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 179

  14. 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 179