write a driver program with a main method to read in data for five pets and dispaly the following data: name of smallest pet, name of largest pet, name of oldest pet, average weight of the five pet, and average age of the five pet. also, test to see the pets are the same.
import java.util.Scanner;
public class PetConstructorsDemo
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
PetConstructors firstPet = new PetConstructors(); // no- method
System.out.println("My records on the first pet are inaccurate.");
System.out.println("Here is what they currently say:");
firstPet.writeOutput();
System.out.println();
PetConstructors secondPet = new PetConstructors("Fluffy",2,10.0); // 3 parameters
System.out.println("My records on the second pet are inaccurate.");
System.out.println("Here is what they currently say:");
secondPet.writeOutput();
System.out.println();
PetConstructors thirdPet = new PetConstructors("Max"); // 1 parameter - name
System.out.println("My records on the third pet are inaccurate.");
System.out.println("Here is what they currently say:");
thirdPet.writeOutput();
System.out.println();
PetConstructors fourthPet = new PetConstructors(5); // 1 parameter - age
System.out.println("My records on the fourth pet are inaccurate.");
System.out.println("Here is what they currently say:");
fourthPet.writeOutput();
System.out.println();
PetConstructors fifthPet = new PetConstructors(100.0); // 1 parameter - weight
System.out.println("My records on the fifth pet are inaccurate.");
System.out.println("Here is what they currently say:");
fifthPet.writeOutput();
System.out.println();
System.out.println("Please enter the correct name for the fifth pet:");
String correctName = keyboard.nextLine();
fifthPet.setName(correctName);
System.out.println("Please enter the correct pet age:");
int correctAge = keyboard.nextInt();
fifthPet.setAge(correctAge);
System.out.println("Please enter the correct pet weight:");
double correctWeight = keyboard.nextDouble();
fifthPet.setWeight(correctWeight);
System.out.println("");
System.out.println("My updated records for the fifth pet now say:");
fifthPet.writeOutput();
}
}
public class PetConstructors
{
private String name;
private int age; //in years
private double weight;//in pounds
/*public PetConstructors()
{
name = "No name yet.";
age = 0;
weight = 0;
}*/
public PetConstructors()
{
this.setPet("No name yet.",0,0);
}
public PetConstructors(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 PetConstructors(String initialName)
{
name = initialName;
age = 0;
weight = 0;
}
public PetConstructors(int initialAge)
{
name = "No name yet.";
weight = 0;
if (initialAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = initialAge;
}
public PetConstructors(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 setPet(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 void setName(String newName)
{
name = newName; //age and weight are unchanged.
}
public void setAge(int newAge)
{
if (newAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = newAge; //name and weight are unchanged.
}
public void setWeight(double newWeight)
{
if (newWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = newWeight; //name and age are unchanged.
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getWeight()
{
return weight;
}
public void writeOutput()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age + " years");
System.out.println("Weight: " + weight + " pounds");
}
}