Pet Record Tester

read in data for five pets

Page 1 of 1

1 Replies - 3029 Views - Last Post: 17 October 2005 - 11:27 AM Rate Topic: -----

#1 ineedjavahelp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-October 05

Pet Record Tester

Posted 17 October 2005 - 11:15 AM

this is the pet recor class

public class PetRecord
{
private String name;
private int age;//in years
private double weight;//in pounds
public void writeOutput()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age + " years");
System.out.println("Weight: " + 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 set(String newName)
{
name = newName; //age and weight are unchanged.
}

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 set(int newAge)
{
if (newAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = newAge;
//name and weight are unchanged.
}

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 set(double newWeight)
{
if (newWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = newWeight;
//name and age are unchanged.
}

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

String getName()
{
return name;
}

int getAge()
{
return age;
}

double getWeight()
{
return weight;
}


}



now i need to do a PetRecord Tester which tests name of the smallest pet, name of the largest pet, name of the oldest pet, name of the youngest pet, a verage weight of the five pets, and average age of the five pets. i do not know how to begin writting this program. i need to use arrays and i am not sure what that is.

Is This A Good Question/Topic? 0
  • +

Replies To: Pet Record Tester

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Pet Record Tester

Posted 17 October 2005 - 11:27 AM

An array is simply a collection of objects of the same type (or sometimes different!). You'll want to declare an array, or group of objects of type PetRecord.
PetRecord[] myPets;
myPets = new PetRecord[5];


You'll then access each of the objects and their methods in order to assign values, and loop to get averages and the like.

For example, to get the average age, you might do something like this:
int avage = 0;
for(int i=0;i<myPets.length;i++)
{
   avage += myPets[i].GetAge();
}
System.out.println("Average age: " + avage/myPets.length );


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1