-1

I have to override hashCode method in my class but i have this error:

cannot find symbol: symbol: variable Objects

Also, how can i add string inventor comparison in equals my code :

public abstract class ThingMadeByHuman {

    protected String inventor;
    protected int yearOfInvention;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o != null || getClass() != o.getClass()) {
            return false;
        }
        ThingMadeByHuman thingMadeByHuman = (ThingMadeByHuman) o;
        return Integer.compare(thingMadeByHuman.yearOfInvention, yearOfInvention) == 0;
    }

    @Override
    public String toString() {
        return "ThingMadeByHuman: inventor:" + this.inventor + " yearOfInvention:" + this.yearOfInvention;
    }

    @Override
    public int hashCode() {
        return Objects.hash(yearOfInvention) + super.hashCode();
    }

    public ThingMadeByHuman(String inventor, int yearOfInvention) {
        this.yearOfInvention = yearOfInvention;
        this.inventor = inventor;
    }
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
miclelov
  • 35
  • 5

1 Answers1

2

You probably need to import the Objects class. See the Java docs. See tutorial by Oracle.

import java.util.Objects ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Evgeny Bovykin
  • 2,572
  • 2
  • 14
  • 27