package ids401.labtwo;
public class Pet {
private String name;
private int age; // in years
private double weight; // in pounds
public Pet() // default constructor
{
name = "XXXXXX";
age = 0;
weight = 0;
}
public Pet(String name, int age, double weight)
{
this.name = name;
this.age = age;
this.weight = weight;
}
public static String getLargestPet(Pet[]pet)
{
String largestName = pet[0].name;
double maxValue = pet[0].weight;
for(int i = 0; i<pet.length;i++){
if(pet[i].weight>maxValue){
largestName = pet[i].name;
}
}
return largestName;
}
public static String getSmallestPet(Pet[]pet)
{
String smallestName = pet[0].name;
double minValue = pet[0].weight;
for(int i = 0; i<pet.length;i++){
if(pet[i].weight<minValue){
smallestName = pet[i].name;
}
}
return smallestName;
}
package ids401.labtwo;
public class PetTest {
public static void main(String[] args) {
int MAX_NUMBER =5;
Pet pet [] = new Pet[MAX_NUMBER];
pet[0]= new Pet("Timber",5,120);
pet[1]= new Pet("Piper",2,20);
pet[2]= new Pet("Reba",4,50);
pet[3]= new Pet("Fluff",1,30);
pet[4]= new Pet("Wilson",9,75);
System.out.println("Largest Pet = "+ Pet.getLargestPet(pet));
System.out.println("Smallest Pet = "+ Pet.getSmallestPet(pet));

New Topic/Question
Reply




MultiQuote






|