-2

For my project I have to modify this method:

/**
     * Determines if this position equals the specified position
     * 
     * @return true of this position and the specified position have the same row
     *         and column value
     */
    @Override
    public boolean equals(Object pos) {
        return false;
    }

How would I fix this? I know to modify the code, but I'm having issues with understanding how to deal with 'pos' being an object.

In the Position class there is two methods called 'getRow' and 'getCol.' How could I do this?

  • 1
    You would probably have to typecast `pos` to whatever type your class is (presumably `Position`, and then call `getRow` and `getCol` after. – Green Cloak Guy Feb 08 '21 at 22:38
  • possible dupicate of [Overriding the java equals() method - not working?](https://stackoverflow.com/questions/185937/overriding-the-java-equals-method-not-working) – geocodezip Feb 08 '21 at 22:42

1 Answers1

0

There are a number of ways to do it. This is one:

public class Position {
    
    private int column;
    private int row;

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Position position = (Position) o;
        return column == position.column && row == position.row;
    }
}
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47