Java Program
Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue
- String animal : empty string
- int lapsRan : 0
- boolean resting : false
- boolean eating : false
- double energy : 100.00
For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors:
1. Constructor 1 – accepts a String with the name of the animal, update the animal property to the value passed in by the user
2. Constructor 2 – accepts no value (user created an object without passing in a string). In this constructor update the animal string to dog
Implement the following methods in your class:
1. Public static int runLap
a. Set eating to false if it’s true
b. Check to see if resting is false and energy is greater than 20, if so then increment the lapsRan by 1 and subtract 20 from energy, otherwise print to the screen “The animal is tired, it must rest”
c. Call the checkEnergy method from here
2. Private static void checkEnergy
a. This method will check if the energy dropped below 0, if so update the resting variable to true and reset energy to 100.00
3. Public static void eatFood
a. If the resting value is true update to false
b. If the eating variable is set to true, print to the screen “The animal is already eating” otherwise set the variable to false and print to the screen “The animal is now eating”.
i. Use the animal string value in place of animal
c. Reset energy to 100
4. Implement all getter/setter methods as mentioned above.
Program
public class AnimalTrainer
{
private String animal = "";
private int lapsRan = 0;
private boolean resting = false;
private boolean eating = false;
private double energy = 100.0;
//constructor
public AnimalTrainer(String name)
{
animal = name;
}
//another constructor
public AnimalTrainer()
{
animal = "dog";
}
//public method runLap
public static int runLap(AnimalTrainer at)
{
if(at.eating)
at.eating =
false;
if(!at.resting && at.energy
> 20){
at.lapsRan++;
at.energy =
at.energy - 20;
}
else
System.out.println ("The " + at.animal + " is tired, it must
rest");
checkEnergy(at);
return at.lapsRan;
}
//private method checkEnergy
private static void checkEnergy(AnimalTrainer
at)
{
if(at.energy<0){
at.resting =
true;
at.energy =
100.00;
}
}
//method eatFood
public static void eatFood(AnimalTrainer at)
{
if(at.resting)
at.resting =
false;
if(at.eating)
System.out.println ("The " + at.animal + " is already
eating");
else{
System.out.println ("The " + at.animal + " is now eating");
at.eating =
true;
at.energy =
100.00;
}
}
//getter methods
public String getAnimal(){return animal;}
public int getLapsRan(){return lapsRan;
}
public boolean getResting(){return
resting; }
public boolean getEating() { return
eating; }
public double getEnergy() { return energy;}
//setter methods
public void setAnimal(String name){ animal =
name;}
public void setLapsRan(int laps){ lapsRan =
laps; }
public void setResting(boolean rest){ resting =
rest; }
public void setEating(boolean eat) { eating =
eat; }
public void setEnergy(double ener) { energy =
ener;}
}
Get Answers For Free
Most questions answered within 1 hours.