Limited access

Pets are considered to be equal if they have exactly the same name, type, age, and weight.

Consider the following definition of a Pet.

public class Pet
{
private String type, name;
private int age;
private int weight;

public Pet(String typ, String nam, int howOld, int wt)
{
type = typ;
name = nam;
age = howOld;
weight = wt;
}
// other data and methods not shown
}


Which of the following methods would correctly allow us to check to see if two pets pet1 and pet2 were equal using the syntax: if(pet1.equals(pet2))?

A
public boolean equals(Pet other)
{
if (name.equals(other.name) ||
type.equals(other.type) ||
age==other.age ||
weight==other.weight)
{
return true;
}
return false;
}

B
public boolean equals(Pet other)
{
if (name.equals(other.name) &&
type.equals(other.type) &&
age==other.age &&
weight==other.weight)
{
return true;
}
}

C
public int equals(Object obj)
{
Pet other = (Pet) obj;
if (name.equals(other.name) &&
type.equals(other.type) &&
age==other.age &&
weight==other.weight)
{
return 0;
}
return 1;
}

D
public boolean equals(Object obj)
{
Pet other = (Pet) obj;
if (name.equals(other.name) )
return true;
else
if (type.equals(other.type))
return true;
else
if (age==other.age)
return true;
else
if (weight==other.weight)
return true;
else
return false;
}

E
public boolean equals(Object obj)
{
Pet other = (Pet) obj;
if (name.equals(other.name) &&
type.equals(other.type) &&
age==other.age &&
weight==other.weight)
{
return true;
}
return false;
}

Select an assignment template