If you want to flip the coordinates in place (so calling p.flip()
modifies Point p
, then this should work:
public class Point {
private int x;
private int y;
public void flip() {
int tmp = x;
x = -y;
y = -tmp;
}
}
If you want the flip()
method to return a new point with the coordinates flipped, then you can do this:
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point flip() {
return new Point(-y, -x);
}
}