Added Java lab programs

This commit is contained in:
2025-12-17 16:36:20 +05:30
parent ac2904f5db
commit 2f4beade3b
46 changed files with 882 additions and 0 deletions
+53
View File
@@ -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);
}
}