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
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
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();...
Code in Java Create a queue class to store integers and implement following methods: 1) void...
Code in Java Create a queue class to store integers and implement following methods: 1) void enqueue(int num): This method will add an integer to the queue (end of the queue). 2) int dequeue(): This method will return the first item in the queue (First In First Out). 3) void display(): This method will display all items in the queue (First item will be displayed first). 4) Boolean isEmpty(): This method will check the queue and if it is empty,...
[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...
Create a field with the boolean value of goodBoi. In the constructor, set your good boi...
Create a field with the boolean value of goodBoi. In the constructor, set your good boi to being false, as all dogs has deserve being called a good boiii. Make a method makeGoodBoi, which sets the good boi value to being true. Make a method isGoodBoi which returns the value the good boi field (hint: it returns a boolean value). Now we need a method called whosAGoodBoi. If the doggo is indeed a good boi, then print: "[doggo] is such...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
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:...
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....
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...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT