1 / 14

Animal

Animal. public abstract class Animal { private int weight ; public Animal( int lb) { // constructor; weight = lb; } public abstract String is_a(); // What animal is this; public abstract String eats(); // the food it eats;

cuyler
Download Presentation

Animal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

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

More Related