mirror of
https://github.com/Manoj-HV30/OOPS-lab-codes.git
synced 2026-05-16 19:35:25 +00:00
Added Java lab programs
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user