-1

I'm trying to override the equals() method that takes an Object as input.

I have the following classes in the same package

public class Herd{
   int count;
   boolean exists;

In the class that is overriding the method, I am trying to compare whether the Object matches the variable in position, rank, and if they are the same Class

public class Animal{
   private Herd lot;
   private int rank;
   public boolean equals(Object animl) {
        if(this.getClass() == animl.getClass() && this.rank == animl.rank && this.lot == animl.**lot**) { 
            return true;
        }
        return false;
    }

I know that to compare I will use an object that has all these parameters, however, in the Animal class itself it says

for animl.rank "rank cannot be resolved or is not a field"
for animl.lot "lot cannot be resolved or is not a field"

I tried downcasting i.e. (... == (Animal) animl.rank) but it gives me an incompatible operand types error. I also try casting the rank into an int, but it gives me the above problem.

Any help appreciated.

Cencoroll
  • 37
  • 1
  • 5

4 Answers4

0

You need to correctly cast your animl object:

public boolean equals(Object animl) {
        if(this.getClass() == ((Animal) animl).getClass() && this.rank == ((Animal) animl).rank && this.lot == ((Animal) animl).lot) { 
            return true;
        }
        return false;
    }

I think it would be better if you use equals instead of =:

public boolean equals(Object animl) {
        if(this.getClass() == ((Animal) animl).getClass() && this.rank.equals( ((Animal) animl).rank) && this.lot.equals( ((Animal) animl).lot)) { 
            return true;
        }
        return false;
    }
Ismail
  • 2,322
  • 1
  • 12
  • 26
0

Try this :

   private Herd lot;
   private int rank;
   public boolean equals(Object animl) {
        if(!(animl instanceof Animal))) {
            return false;
        }
        Animal an = (Animal)animl;
       return this.rank == an.rank && this.lot == an.lot;
    }

You might need getters for those attributes. I didn't compile or test any of this.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
0

Method equals in class Animal should be overridden like this:

  1. Check for null and class equality
  2. Cast the object to compare to Animal
  3. Compare fields of Animal class, paying attention to using equals for the Herd
public class Animal {
    // ...

    @Override
    public boolean equals(Object o) {
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        if (this == o) return true;

        Animal animal = (Animal) o;
        return this.rank == animal.rank && 
               this.lot != null && this.lot.equals(animal.lot);
    }
}

Similarly, method equals may need to be overridden in Herd:

public class Herd {
    // ...

    @Override
    public boolean equals(Object o) {
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        if (this == o) return true;

        Herd herd = (Herd) o;
        return this.count == herd.count && this.exists == herd.exists;
    }
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
0

Comparison will not work this way. The messages:

for animl.rank "rank cannot be resolved or is not a field"
for animl.lot "lot cannot be resolved or is not a field"

are correct because there are no such properties in Object class. They only exist in your Animal class.

That said, you will need to explicitly cast the Object to Animal before doing the actual comparison. Try this:

public class Animal{
   private Herd lot;
   private int rank;
   public boolean equals(Object animl) {
     Animal animal = (Animal) animl;
        if(this.getClass() == animal.getClass() && this.rank == animal.rank && this.lot == animal.lot) { 
            return true;
        }
        return false;
    }

On a side note, you may need to do nested comparison for this.lot == animal.lot