Question

Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...

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.

Homework Answers

Answer #1

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;}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1....
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. // code from Q1: interface Vehicle { void Start();...
For the following class Book: 1- Write a default constructor to give default values (title= Intro...
For the following class Book: 1- Write a default constructor to give default values (title= Intro to Programming and price = 20) to the class’s variables. 2 - Write a parameterized constructor to allow the user to initialize the class variables. 3- Write getter & setter for each variable of this class. 4- Then write a main code to create an instance of this class using parameterized constructor, ask user for inputs to initialize the variables of this instance and...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
IN JAVA PLEASE Design and implement an ADT CreditCard that represents a credit card. The data...
IN JAVA PLEASE Design and implement an ADT CreditCard that represents a credit card. The data of the ADT should include Java variables for the customer name, the account number, the next due date, the reward points, and the account balance. The initialization operation should set the data to client-supplied values. Include operations for a credit card charge, a cash advance, a payment, the addition of interest to the balance, and the display of the statistics of the account. Be...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Java Programming In this lab students continue to write programs using multiple classes. By the end...
Java Programming In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to Write classes that use arrays and ArrayLists of objects as instance variables Write methods that process arrays and ArrayLists of objects Write getter and setter methods for instance variables Write methods using object parameters and primitive types Question- class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...