Files
2025-12-17 16:39:23 +05:30

54 lines
1.1 KiB
Java

class MyPoint {
private int x;
private int y;
// No-argument constructor
MyPoint() {
x = 0;
y = 0;
}
// Overloaded constructor
MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
// Set both x and y
void setXY(int x, int y) {
this.x = x;
this.y = y;
}
// Return x and y as a 2-element array
int[] getXY() {
return new int[] { x, y };
}
// String representation
public String toString() {
return "(" + x + ", " + y + ")";
}
// Distance to given (x, y)
double distance(int x, int y) {
return Math.sqrt(
(this.x - x) * (this.x - x) + (this.y - y) * (this.y - y)
);
}
// Distance to another MyPoint object
double distance(MyPoint another) {
return Math.sqrt(
(this.x - another.x) * (this.x - another.x) +
(this.y - another.y) * (this.y - another.y)
);
}
// Distance to origin (0, 0)
double distance() {
return Math.sqrt(x * x + y * y);
}
}