NEED IN JAVA A point in the xy plane is represented by its x
NEED IN JAVA
A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design the Class Point that can store and process a point in the x-y plane. You should then perform operations on a point, such as showing the point, setting the coordinates of the point, printing the coordinates of the point, returning the x-coordinate, and returning the y-coordinate. Also, write a test program to test various operations on a point.
Solution
//Point.java
public class Point {
private int x; // x-coordinate
private int y; // y-coordinate
// default constructor point
public Point() {
x = 0;
y = 0;
}
// parameterized point class
public Point(int x ,int y) {
setX(x);
setY(y);
}
//Mutator methods
public void setX(int x) { this.x=x; }
public void setY(int y) { this.y=y; }
// accessor methods
public int getX() { return x; }
public int getY() { return y; }
//Override toString method to print x and y values
public String toString() {
return \"Point (\"+x+\",\"+y+\")\";
}
}
--------------------------------------------------------------------------------------------
//DriverPoint.java
/**
* Tester java program DriverPoint that
* test the methods of Point class
* */
//DriverPoint.java
public class DriverPoint {
public static void main(String[] args) {
//Create an instance of Point class
Point p=new Point();
//Set x-coordinate
p.setX(5);
//Set y-coordinate
p.setY(15);
System.out.println(\"Point coordinates\");
System.out.println(\"X-value : \"+p.getX());
System.out.println(\"y-value : \"+p.getY());
//calling toString to print the point object
System.out.println(p.toString());
}
}
--------------------------------------------------------------------------------------------
Sample Output:
Point coordinates
X-value : 5
y-value : 15
Point (5,15)

