Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//The answer must have balanced parentheses
class Point { int x; int y;
  public Point(int x, int y) {this.x = x; this.y = y;}
  [???]
}
public class Exercise{

  public static void main(String [] arg){
    Point p1 = new Point(1,2);
    Point p2 = new Point(1,2);
    assert p1.equals(p2);
  }
}


What I have tried:

I have tried employing the use of a super keyword but that requires I write a new class that extends class Point.

Is there a way to solve this problem without doing so ?
Posted
Updated 29-Apr-21 5:24am

1 solution

At a guess from looking at your code, you need to write an equals method. As it stands your call to equals will invoke the base object method that just compares the two references p1 and p2. But I assume what you really want to know is whether the two points are referring to the exact same logical position.
 
Share this answer
 
Comments
Member 13469023 29-Apr-21 11:43am    
Thank you very much !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900