Writing Methods

AP Computer Science A· difficulty 4/5

public boolean equals(Object o) {
    if (!(o instanceof Box)) return false;
    Box other = (Box) o;
    return /* missing */;
}

Which expression compares the width fields correctly?

  • A

    width.equals(other.width)

  • B

    this.width = other.width

  • C

    this.width == other.width

    check_circle
  • D

    this == other

Explanation

width is an int field, so use == to compare values; .equals applies to objects, and = is assignment.

Want 10 more like this — adaptive to your weak spots?

Related questions