same xcoordinates compare their ycoordinates to be written
* same x-coordinates, compare their y-coordinates.
to be written in java
| * Write a program that meets the following requirements: | |
| * Define a class named Point with two data fields x and y to represent a point’s x- and y-coordinates. | |
| * Implement the Comparable interface for com- paring the points on x-coordinates. If two points have the | |
| * same x-coordinates, compare their y-coordinates. |
Solution
Point.java
public class Point implements Comparable<Point>{
private int x;
private int y;
public Point(){
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int compareTo(Point p) {
if(this.x != p.x)
return this.x - p.x;
else
return this.y - p.y;
}
}
