Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public class PetRecord { private String name; private int age;//in years private double weight;//in pounds public String toString( ) { return (Name: + name
public class PetRecord
{
private String name;
private int age;//in years
private double weight;//in pounds
public String toString( )
{
return ("Name: " + name + " Age: " + age + " years"
+ " Weight: " + weight + " pounds");
}
public PetRecord(String initialName, int initialAge,
double initialWeight)
{
name = initialName;
if ((initialAge
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = initialAge;
weight = initialWeight;
}
}
public void set(String newName, int newAge, double newWeight)
{
name = newName;
if ((newAge
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = newAge;
weight = newWeight;
}
}
public PetRecord(String initialName)
{
name = initialName;
age = 0;
weight = 0;
}
public void setName(String newName)
{
name = newName;
}
public PetRecord(int initialAge)
{
name = "No name yet.";
weight = 0;
if (initialAge
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = initialAge;
}
public void setAge(int newAge)
{
if (newAge
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = newAge;
}
public PetRecord(double initialWeight)
{
name = "No name yet";
age = 0;
if (initialWeight
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = initialWeight;
}
public void setWeight(double newWeight)
{
if (newWeight
Using the PetRecord class stored on Canvas, write a driver program (main) with a main method to read in data from the user for five Pets and display the following data: name of smallest pet, name of largest pet, name of oldest pet, name of youngest pet, average weight of the five pets, and average age of the five pets. Also, test to see if any of the five pets are the same {
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = newWeight;
}
public PetRecord( )
{
name = "No name yet.";
age = 0;
weight = 0;
}
public String getName( )
{
return name;
}
public int getAge( )
{
return age;
}
public double getWeight( )
{
return weight;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started