Sale!

Class Animal

$30.00

Category:
Rate this product

Write an application that models a brutal war for survival among small animals.
All the animals in this lab inherit from the abstract class Animal. Animal has a name and a weight in grams. It has a public method eat, which takes a double parameter that represents the weight in grams of a food item and adds that much to the animal’s weight. It also has a call() method. Since different animals have different calls, call() must be an abstract method.
The abstract class Prey extends Animal by adding a flee() method. Since different Prey animals flee differently, this method must be abstract.
The abstract class Predator extends Animal by adding a predate() method. Predate() takes a reference to a Prey animal (one that extends Prey), calls the instance Animal’s (that is, the Predator’s) eat() method with the weight of the Prey animal as parameter, and shows a message similar to “Thor The Wolf eats Bambi,” using the actual names of the Predator and Prey animals.
The concrete class Cat extends predator and implements call() by showing a message like “Fifi says ‘Meow'”. It overrides toString() to return a message similar to “Fifi is a Cat weighing 982.3 grams.”
The concrete classes Bird and Mouse extend Prey in appropriate ways.
It is up to you to decide where to put constructors, getters, setters, and any other necessary methods.
Use this driver class and make sure the results are reasonable:

public class AnimalDriver {

public static void main(String[] arg) {
Random r = new Random();
Cat precious = new Cat(“Precious”, 1223.34);

List <Prey prey = new ArrayList<Prey();
Mouse m1 = new Mouse(25.3, “Mickey”);
Mouse m2 = new Mouse(28.0, “Minnie”);
Bird b1 = new Bird(50.9, “Daisy”);
Bird b2 = new Bird(55.1, “Donald”);

prey.add(m1);
prey.add(m2);
prey.add(b1);
prey.add(b2);

System.out.println(precious.toString());
precious.call();

StringBuilder sb = new StringBuilder();
for (Prey p : prey)
sb.append(p.toString() + “\n”);
System.out.println(sb);

for (Prey p : prey) {
p.call();
if (r.nextBoolean() == false)
p.flee();
else
precious.predate(p);
}
}
}

Open chat
Need help?
Hello
Can we help you?