Using the PetRecord class below write a JAVA driver program
Using the PetRecord class below, write a JAVA 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. DO NOT USE ARRAYS TO COMPLETE THIS!
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:
Solution
import java.util.Comparator;
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()));
}
}
/**
* @author
*
*/
class PetAgeComp implements Comparator<PetRecord> {
/*
* (non-Javadoc)
*
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@Override
public int compare(PetRecord o1, PetRecord o2) {
// TODO Auto-generated method stub
return o1.getAge() - o2.getAge();
}
}
/**
* @author
*
*/
class PetWeightComp implements Comparator<PetRecord> {
/*
* (non-Javadoc)
*
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@Override
public int compare(PetRecord o1, PetRecord o2) {
// TODO Auto-generated method stub
if (o1.getWeight() - o2.getWeight() > 0)
return 1;
else
return -1;
}
}
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class PetDriver {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(new File(\"petdata.txt\"));
PetRecord[] petRecords = new PetRecord[5];
int count = 0;
while (scanner.hasNext()) {
String line = scanner.nextLine();
String[] lineArr = line.split(\",\");
PetRecord petRecord = new PetRecord(lineArr[0],
Integer.parseInt(lineArr[1]),
Double.parseDouble(lineArr[2]));
petRecords[count] = petRecord;
count++;
}
System.out.println(\"Pets Data:\");
double sumWeight = 0;
int sumAge = 0;
for (int i = 0; i < petRecords.length; i++) {
System.out.println(petRecords[i]);
sumAge += petRecords[i].getAge();
sumWeight += petRecords[i].getWeight();
}
Arrays.sort(petRecords, new PetWeightComp());
System.out.print(\"Smallest Pet:\");
System.out.println(petRecords[0]);
System.out.print(\"Largest Pet:\");
System.out.println(petRecords[4]);
Arrays.sort(petRecords, new PetAgeComp());
System.out.print(\"Youngest Pet:\");
System.out.println(petRecords[0]);
System.out.print(\"Oldest Pet:\");
System.out.println(petRecords[4]);
System.out.println(\"The average weight of all pets:\"
+ (sumWeight / 5.0));
System.out.println(\"The average age of all pets:\"
+ (double) (sumAge / 5.0));
} catch (Exception e) {
// TODO: handle exception
}
}
}
petdata.txt
Dog,32,32.3
Cat,12,21.3
Rabbit,13,11
Parrot,11,5.2
Duck,3,4.3
OUTPUT:
Pets Data:
Name: Dog Age: 32 years Weight: 32.3 pounds
Name: Cat Age: 12 years Weight: 21.3 pounds
Name: Rabbit Age: 13 years Weight: 11.0 pounds
Name: Parrot Age: 11 years Weight: 5.2 pounds
Name: Duck Age: 3 years Weight: 4.3 pounds
Smallest Pet:Name: Duck Age: 3 years Weight: 4.3 pounds
Largest Pet:Name: Dog Age: 32 years Weight: 32.3 pounds
Youngest Pet:Name: Duck Age: 3 years Weight: 4.3 pounds
Oldest Pet:Name: Dog Age: 32 years Weight: 32.3 pounds
The average weight of all pets:14.819999999999999
The average age of all pets:14.2




