Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Tutorial exercises to complete public class AnimalTester ( public static vold Cr

ID: 3593087 • Letter: T

Question

Tutorial exercises to complete public class AnimalTester ( public static vold Create two interfaces called Movable and Expendable which have the following fields and methods Animal animalis new Animal3 Movable-void move(double distance) animais[1) new CoSherry,19 animals(2) new Tigerf"Maxwelr, 12) for Expendable-void spendEnergyldouble energv) Write an abstract class Animal that implements the two interfaces with fields for age, name, type, enersylevel (double), energySpent (double), distance Travelled (double) and all the appropriate getters, setters and constructors. All animals start with 100 units of energy. The Animal class should also contain the following abstract methods eat(String food) makeSoundg Write three concrete classes-Piranha, Cow, Tiger-which inherit from Animal such that: For eat(String food), if food Expected output: "meat"-replenishes energy by 10 for piranhas and tigers "grass"-replenishes energy by 10 for cows and 5 for piranhas "water - replenishes energy by 5 for cows and tigers This piranha started with 115.0 units of energy This 18 year-old piranha named Steve moved 245.0 metres and " 8166666666666666 units of energy This piranha finished with 106.83333333333333 units of energy For makeSoundo: his cow started with 115.0 units of energy This 19-year-old cow named Sherry moved 245.0 metres and us 2AS units of energy This cow finished with 112.55 units of energy Cows print "Moo . Tigers print "Roar Piranhas print "Bloop Finally, implement the methods of the two interfaces such that Cows spend 1 energy per 100 distance Tigers spend 1 energy per 10 distance This tiger started with 115.0 units of energs This 12-year-old tiger named Maxwell moved 245.0 metres an 245 units of energy This tiger finished with 90.5 units of energy s spend 1 energy per 30 distance Print the energy spent and distance covered every time the move) method is invoked.

Explanation / Answer

//abstract class Animal definition

abstract class Animal

{

//Instance variables to store data

int age;

String name;

String type;

String sound;

double energyLevel;

double energySpent;

double distanceTraveled;

//Default constructor

Animal()

{

name = "";

type = "";

energyLevel = 100;

energySpent = 0;

distanceTraveled = 0;

}//End of default constructor

//Parameterized constructor

Animal(String na, int a, String t)

{

name = na;

age = a;

type = t;

energyLevel = 100;

}//End of default constructor

//abstract method declaration

abstract void eat(String food);

abstract void makeSound();

abstract void move(double d);

}//End of class

//class Piranha derived from Animal

class Piranha extends Animal

{

//Parameterized constructor

Piranha(String na, int a)

{

//Calls base class constructor

super(na, a, "Piranha");

}//End of constructor

//Overrides method eat

void eat(String food)

{

//Checks food if equals meat then add 10 to energyLevel

if(food.equals("meat"))

energyLevel += 10;

//Checks food if equals grass then add 5 to energyLevel

if(food.equals("grass"))

energyLevel += 5;

}//End of method

//Overrides method make sound

void makeSound()

{

sound = "Bloop!";

}//End of method

//Overrides method move

void move(double d)

{

distanceTraveled = d;

energySpent = (distanceTraveled / 30);

}//End of method

}//End of class

//class Cow derived from Animal

class Cow extends Animal

{

//Parameterized constructor

Cow(String na, int a)

{

//Calls base class constructor

super(na, a, "Cow");

}//End of constructor

//Overrides method eat

void eat(String food)

{

//Checks food if equals grass then add 10 to energyLevel

if(food.equals("grass"))

energyLevel += 10;

//Checks food if equals water then add 5 to energyLevel

if(food.equals("water"))

energyLevel += 5;

}//End of method

//Overrides method makeSound

void makeSound()

{

sound = "Moo!";

}//End of method

//Overrides method move

void move(double d)

{

distanceTraveled = d;

energySpent = (distanceTraveled / 100);

}//End of method

}//End of method

//class Tiger derived from Animal

class Tiger extends Animal

{

//Overrides method eat

Tiger(String na, int a)

{

//Calls base class constructo

super(na, a, "Tiger");

}//End of constructor

//Overrides method eat

void eat(String food)

{

//Checks food if equals meat then add 10 to energyLevel

if(food.equals("meat"))

energyLevel += 10;

//Checks food if equals water then add 5 to energyLevel

if(food.equals("water"))

energyLevel += 5;

}//End of method

//Overrides method makeSound()

void makeSound()

{

sound = "Roar!";

}//End of method

//Overrides method move()

void move(double d)

{

distanceTraveled = d;

energySpent = (distanceTraveled / 10);

}//End of method

}//End of class

//Driver class definition

public class AnimalTester

{

//main method definition

public static void main(String ss[])

{

//Creates an array of object of Animal class

Animal animals[] = new Animal[3];

//Instaniate the object to Piranha class

animals[0] = new Piranha("Seve", 18);

//Instaniate the object to cow class

animals[1] = new Cow("Sherry", 19);

//Instaniate the object to Tiger class

animals[2] = new Tiger("Maxwell", 12);

//Using enhanced for loop loops through 3 objects of Animal class

for(Animal a : animals)

{

//Calls the methods

a.makeSound();

a.eat("meat");

a.eat("grass");

a.eat("water");

a.move(245.0);

//Displays information

System.out.println(a.sound);

System.out.println("This " + a.type + " started with " + a.energyLevel + " units of energy");

System.out.println("This " + a.age + " - years-old " + a.type

+ " named " + a.name + " moved " + a.distanceTraveled

+ " and spent " + a.energySpent + " units of energy");

System.out.println("The piranha finished with " + (a.energyLevel - a.energySpent) + " units of energy");

}//End of for loop

}//End of main method

}//End of class

Sample Run:

Bloop
This Piranha started with 115.0 units of energy
This 18 - years-old Piranha named Seve moved 245.0 and spent 8.166666666666666 units of energy
The piranha finished with 106.83333333333333 units of energy
Moo
This Cow started with 115.0 units of energy
This 19 - years-old Cow named Sherry moved 245.0 and spent 2.45 units of energy
The piranha finished with 112.55 units of energy
Roar
This Tiger started with 115.0 units of energy
This 12 - years-old Tiger named Maxwell moved 245.0 and spent 24.5 units of energy
The piranha finished with 90.5 units of energy

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote