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

Using the PetRecord class stored on BlackBoard , write a driver program with a m

ID: 3695756 • Letter: U

Question

Using the PetRecord class stored on BlackBoard, write a driver program with a main method to read in data for five Pets from a data file. The data file will contain the name, age, and weight of each pet, fields separated by commas, one pet per line in the file. Create a PetRecord as each line is read.

Display the following information:

The name, age, and weight of each pet.

The name, age, and weight of smallest pet and the name, age, and weight of largest pet.

The name, age, and weight of the oldest pet and the name, age, and weight of the youngest pet.

The average weight of all pets.

The average age of all pets.

Also, test to see if the first pet in the data file is the same as any of the other pets using the equals() method. Be sure to include an example of this test when you PrintScreen your results.

PetRecord class:

Explanation / Answer

public class PetRecord { // Instance variables private String name; private int age; //in years private double weight; //in pounds // Default values for instance variables private static final String DEFAULT_NAME = "No name yet." ; private static final int DEFAULT_AGE = -1 ; private static final double DEFAULT_WEIGHT = -1.0 ; /*************************************************** * Constructors to create objects of type PetRecord ***************************************************/ // no-argument constructor public PetRecord( ) { this(DEFAULT_NAME, DEFAULT_AGE, DEFAULT_WEIGHT) ; } // only name provided public PetRecord(String initialName) { this(initialName, DEFAULT_AGE, DEFAULT_WEIGHT) ; } // only age provided public PetRecord(int initialAge) { this(DEFAULT_NAME, initialAge, DEFAULT_WEIGHT) ; } // only weight provided public PetRecord(double initialWeight) { this(DEFAULT_NAME, DEFAULT_AGE, initialWeight) ; } // full constructor (all three instance variables provided) public PetRecord(String initialName, int initialAge, double initialWeight) { setName(initialName) ; setAge(initialAge) ; setWeight(initialWeight) ; } /**************************************************************** * Mutators and setters to update the Pet. Setters for age and * weight validate reasonable weights are specified ****************************************************************/ // Mutator that sets all instance variables public void set(String newName, int newAge, double newWeight) { setName(newName) ; setAge(newAge) ; setWeight(newWeight) ; } // Setters for each instance variable (validate age and weight) public void setName(String newName) { name = newName; } public void setAge(int newAge) { if ((newAge < 0) && (newAge != DEFAULT_AGE)) { System.out.println("Error: Invalid age."); System.exit(99); } age = newAge; } public void setWeight(double newWeight) { if ((newWeight < 0.0) && (newWeight != DEFAULT_WEIGHT)) { System.out.println("Error: Invalid weight."); System.exit(98); } weight = newWeight; } /************************************ * getters for name, age, and weight ************************************/ public String getName( ) { return name; } public int getAge( ) { return age; } public double getWeight( ) { return weight; } /**************************************************** * toString() shows the pet's name, age, and weight * equals() compares all three instance variables ****************************************************/ public String toString( ) { return ("Name: " + name + " Age: " + age + " years" + " Weight: " + weight + " pounds"); } public boolean equals(PetRecord anotherPetRecord) { if (anotherPetRecord == null) { return false ; } return ((this.getName().equals(anotherPetRecord.getName())) && (this.getAge() == anotherPetRecord.getAge()) && (this.getWeight() == anotherPetRecord.getWeight())) ; } }
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