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

Create an array of Pet objects. You want to keep track of all of your friends’ p

ID: 3929866 • Letter: C

Question

Create an array of Pet objects. You want to keep track of all of your friends’ pets. Write a class called AllPets.java using PetRecord.java class as the array type. Allow the array to hold at least 4 records.

Ideally, we would read the pet data from a text file or allow the user to enter it from the keyboard, but to make writing & testing of your program easier, just hard code the pet details (name, age, & weight) into your main method.

After creating and setting the pet data, be sure to print all PetRecords to the screen to verify the contents of the array.

Explanation / Answer

public class PetRecord1
{
private String name;
private int age;//in years
private double weight;//in pounds
private int Smallest;
private int Largest;
private int Oldest;
private int Youngest;
private double AverageWeight;
private double AverageAge;

public void writeOutput( )
{
System.out.println("* Name: " + name);
System.out.println("* Age: " + age + " years");
System.out.println("* Weight: " + weight + " pounds");
}

public PetRecord1(String initialName, int initialAge,
double initialWeight)
{
name = initialName;
if ((initialAge < 0) || (initialWeight < 0))
{
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 < 0) || (newWeight < 0))
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = newAge;
weight = newWeight;
}
}

public PetRecord1(String initialName)
{
name = initialName;
age = 0;
weight = 0;
}

public void set(String newName)
{
name = newName; //age and weight are unchanged.
}

public PetRecord1(int initialAge)
{
name = "No name yet.";
weight = 0;
if (initialAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = initialAge;
}

public void set(int newAge)
{
if (newAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = newAge;
//name and weight are unchanged.
}

public PetRecord1(double initialWeight)
{
name = "No name yet";
age = 0;
if (initialWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = initialWeight;
}

public void set(double newWeight)
{
if (newWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = newWeight; //name and age are unchanged.
}

public PetRecord1( )
{
name = "No name yet.";
age = 0;
weight = 0;
}

public String getName( )
{
return name;
}

public int getAge( )
{
return age;
}

public double getWeight( )
{
return weight;
}

//all new stuff added

public int getSmallest()
{
return Smallest;
}

public int getLargest()
{
return Largest;
}

public int getOldest()
{
return Oldest;
}

public int getYoungest()
{
return Youngest;
}

public double getAverageWeight()
{
return AverageWeight;
}

public double getAverageAge()
{
return AverageAge;
}

}

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