Write an application in java that models a zoo that contains Lions and Ducks. Bo
ID: 3862302 • Letter: W
Question
Write an application in java that models a zoo that contains Lions and Ducks. Both Lion and Duck are concrete classes that extend the abstract class Animal. You must be able to call the following methods on any Animal:
public double getWeight() works the same for all Animals
public String call() prints out a different String for each type of Animal
-Lion's call() method prints "roar" and Duck's prints "quack"
public String attack() prints a different String for each type of animal
-Lion's attack() prints the String "bares teeth and bites" and Duck's prints "pecks with its ferocious bill"
public String toString() returns Strings like these:
0.6830105287458925 kg duck
135.9492664745286 kg lion
Think out what variables, getters, setters, and constructors your classes will need. All ducks have the same call, and all Lions have the same call, but different animals of each class have different weights.
Write the class Zoo so that it meets these specifications:
Contains a List of Animals
public void addAnimal(Animal a) takes a reference to an Animal (an instance of one of the concrete subclasses of Animal) and adds it to the list.
public void listAnimals() prints the message "the zoo contains the following animals:" and then prints each animal's list index and toString() String.
public void makeNoise() runs the call() method for each animal in the list.
public void jailBreak() runs the attack() method for each animal in the list.
The class ZooDriver should hard code some animals, call listAnimals(), call makeNoise(), and then call jailBreak(). You do not need to take user input for this problem. Here is sample output from my ZooDriver for a zoo with only 3 animals:
The zoo contains the following animals:
animal 0 is a 0.6177069624304481 kg duck
animal 1 is a 1.137326599134231 kg duck
animal 2 is a 144.04307883470435 kg lion
animal 0 quacks
animal 1 quacks
animal 2 roars
animal 0 pecks with its ferocious bill
animal 1 pecks with its ferocious bill
animal 2 bares teeth and bites
Explanation / Answer
import java.util.*;
abstract class Animal //base abstract class
{
private double weight;
public Animal(double weight) //constructor
{
this.weight = weight;
}
public double getWeight()
{
return weight;
}
// abstract methods defined in derived classes
public abstract String call();
public abstract String attack();
public abstract String toString();
}
class Lion extends Animal //Lion derived class
{
public Lion(double weight) //constructor sending weight to super class
{
super(weight);
}
public String call()
{
return "roar";
}
public String attack()
{
return "bares teeth and bites";
}
public String toString()
{
return getWeight() + "kg lion";
}
}
class Duck extends Animal //Duck derived class
{
public Duck(double weight) //constructor sending weight argument to super class
{
super(weight);
}
public String call()
{
return "quack";
}
public String attack()
{
return "pecks with its ferocious bill";
}
public String toString()
{
return getWeight() + "kg duck";
}
}
class Zoo
{
private List<Animal> mylist; // list of Animals
public Zoo(List<Animal> mylist) // constructor
{
this.mylist = mylist;
}
public void addAnimal(Animal a) //add animal
{
mylist.add(a);
}
public void listAnimals() //list All animals
{
for(int i=0;i<mylist.size();i++)
System.out.println(mylist.get(i).toString());
}
public void makeNoise() //list all noises made y animals
{
for(int i=0;i<mylist.size();i++)
System.out.println(mylist.get(i).call());
}
public void jailBreak() //list all attacks made by animals
{
for(int i=0;i<mylist.size();i++)
System.out.println(mylist.get(i).attack());
}
}
class ZooDriver
{
public static void main(String[] args)
{
List<Animal> mylist = new ArrayList<Animal>(); // create list
Duck duck1 = new Duck(0.6177069624304481); //add animals to list
Duck duck2 = new Duck(1.137326599134231);
Lion lion1 = new Lion(144.04307883470435);
mylist.add(duck1);
mylist.add(duck2);
mylist.add(lion1);
Zoo z = new Zoo(mylist); //send list of animals to Zoo
// call to Zoo methods
z.listAnimals();
z.makeNoise();
z.jailBreak();
}
}
Output:
0.6177069624304481kg duck
1.137326599134231kg duck
144.04307883470435kg lion
quack
quack
roar
pecks with its ferocious bill
pecks with its ferocious bill
bares teeth and bites
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.