We are give this first program and told to write a second program to do the following:
Write a driver program with a main method to read in data 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. Be sure to include an example of this test when you PrintScreen your results.
Here is the supplied code
Here is my start for the driver program. I instantly run into compiling issue "cannot find symbol method getWeight" - I don't understand this as I used the exact format for name and age, both which compile fine. I am thinking I am totally going in the wrong direction. Any help is appreciated.Code: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"
+ "\nWeight: " + weight + " pounds");
}
public PetRecord(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 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 < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = initialAge;
}
public void setAge(int newAge)
{
if (newAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = newAge;
}
public PetRecord(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 setWeight(double newWeight)
{
if (newWeight < 0)
{
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;
}
}
Gary
Code:import java.util.*;
public class PetRecordTest
{
public static void main(String[] args)
{
// Declare a variable that will be used to
// reference an InventoryItem object.
Dog dogOne;
// The getData method will return a reference
// to an InventoryItem object.
dogOne = getData();
// Display the object's data.
System.out.println("Here is the data you entered:");
System.out.println("Name: "
+ dogOne.getName()
+ " Age: " + dogOne.getAge()
+ " Weight: " + dogOne.getWeight());
}
public static Dog getData()
{
String name; // To hold the dog name
int age; // To hold the age
double weight; // to hold the weight
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the dog name
System.out.print("Enter the dog name: ");
name = keyboard.nextLine();
// Get the age
System.out.print("Enter age: ");
age = keyboard.nextInt();
// Get the weight
System.out.print("Enter weight: ");
weight = keyboard.nextDouble();
// Create an InventoryItem object and return
// a reference to it.
return new Dog(name, age, weight);
}
}
