In: Computer Science
Design a class named GeoPoint to represent a point with x- and
y-coordinates. The class contains:
The data fields x and y that represent the coordinates with gette
and setter methods.
A no-arg constructor that creates a point (0, 0).
A constructor that constructs a point with specified
coordinates.
The method equals(GeoPoint p) that returns true if two GeoPoint
objects have the same x- and y-coordinates.
Write a test program that creates an array of GeoPoint objects. The size of the array is 3, and fill the array with three points: (2, 2), (3, 4), and (5, 5), and then print out the elements of the array.
Code
class GeoPoint
{
private int x,y;
public GeoPoint()
{
x=0;y=0;
}
public GeoPoint(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;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final GeoPoint other = (GeoPoint) obj;
if (this.x != other.x) {
return false;
}
if (this.y != other.y) {
return false;
}
return true;
}
@Override
public String toString() {
return "GeoPoint is {" + x + ","+ y + ')';
}
}
public class Test {
public static void main(String[] args) {
GeoPoint points[]=new GeoPoint[3];
points[0]=new GeoPoint(2,2);
points[1]=new GeoPoint(3,4);
points[2]=new GeoPoint(5,5);
for (int i=0;i<3;i++)
System.out.println(points[i]);
}
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.