AP Computer Science --- Haas --- Pet
Complete the following class which creates pets and keeps track of
their names, type, and weight.
/**
* Class Pet: >>> Code is NOT complete !!! <<<
*
* constructs a pet with 3 fields:
* 1) String name = the name of the pet
* 2) String type = what type of animal it is (dog, cat, cow...)
* 3) int weight = the weight of the pet
*
* class Pet has static variables that keep track of the following
* 1) name and weight of the heaviest pet
* 2) name and weight of the lightest pet
* 3) the average weight
*
* class Pet has a static method which prints out the names and weights
* of the heaviest, and lightest pet, and the average weight
*
* extra challenge !! try to create a instance method called changeWeight(int newWeight)
* which allows you to change the weight of a pet and updates
* the heaviest, lightest, and average accordingly
*/
public class Pet
{
private String name;
private String type;
private int weight;
// >>>>> complete the code <<<<<<
// define static variables to keep track of
// heaviest, lightest, and average weights and names
/**
* constructs a pet with a name, type, and weight
*/
public Pet(String pName, String pType, int pWeight)
{
// >>>>> complete the code <<<<<<
// initialize instance variables and call methods to
// heaviest, lightest, and average weights and names
}
// >>>>> complete the code <<<<<<
// Add more methods as needed below
}
/**
* Tester for class Pet: This class works as is.
*
* Calls the Pet class to create 5 pets and prints
* out the heaviest, lightest, and average weights
*/
public class PetTester
{
public static void main( String[] args)
{
/**
* Prints pet stats: >>> No pets have been created! <<<
*/
Pet.stats();
Pet p1 = new Pet("Rags", "dog", 60);
Pet p2 = new Pet("BooBoo", "cat", 10);
/**
* Prints pet stats:
* >>> The heaviest pet is: Rags, who weighs 60
* >>> The lightest pet is: BooBoo, who weighs 10
* >>> The average pet weight is: 35.0
*/
Pet.stats();
Pet p3 = new Pet("Chopper", "dog", 110);
Pet p4 = new Pet("Mr. Belvedere", "parrot", 1);
Pet p5 = new Pet("Tiger", "cat", 8);
/**
* Prints pet stats:
* The heaviest pet is: Chopper, who weighs 110
* The lightest pet is: Mr. Belvedere, who weighs 1
* The average pet weight is: 37.8
*/
Pet.stats();
}
}