Limited access

Consider the Car class below. Note that two cars are considered to be equal if they have exactly the same miles per gallon.

public class Car
{
private int distance;
private int gallonsUsed;

// Calculate the miles per gallon
public int calcMPG()
{
return (int) ((double) distance / gallonsUsed + 0.5);
}

// other instance variables, constructor, and methods not shown
}


How should the equals method from the Object class be overridden to accomplish this?

A
public boolean equals(Car c)
{
if ((distance / gallonsUsed) == c.(distance / gallonsUsed))
return true;
return false;
}

B
public boolean equals(Car c)
{
if (calcMPG() == c.calcMPG())
return true;
return false;
}

C
public boolean equals(Object obj)
{
Car otherCar = (Car) obj;
return (calcMPG() == otherCar.calcMPG());
}

D
public boolean equals(Object obj)
{
if (calcMPG() == obj.calcMPG())
return true;
return false;
}

E
public boolean equals(Car c)
{
double avg1 = (int) ((double) distance / gallonsUsed + 0.5);
double avg2 = (int) ((double) c.distance / c.gallonsUsed + 0.5);
if (avg1 == avg2)
return true;
return false;
}

Select an assignment template