AssignmentS2

This commit is contained in:
2025-02-25 01:41:01 +03:00
parent 2c4a3a53b9
commit c6f3123b73
12 changed files with 167 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,37 @@
public class Point2D {
private double x, y;
public Point2D (double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double distance(double x, double y) {
return Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2));
}
public double distance(Point2D p) {
return Math.sqrt(Math.pow(this.x - p.getX(), 2) + Math.pow(this.y - p.getY(), 2));
}
public static double distance(Point2D p1, Point2D p2) {
return Math.sqrt(Math.pow(p1.getX() - p2.getX(), 2) + Math.pow(p1.getY() - p2.getY(), 2));
}
public Point2D midpoint(Point2D p) {
double midx = (this.x + p.getX()) / 2;
double midy = (this.y + p.getY()) / 2;
return new Point2D (midx, midy);
}
}

View File

@ -0,0 +1,18 @@
public class demo {
public static void main(String[] args) {
Point2D p1 = new Point2D(3.0, 4.0);
Point2D p2 = new Point2D(6.0, 8.0);
System.out.println("Coordinates of p1: (" + p1.getX() + ", " + p1.getY() + ")");
System.out.println("Coordinates of p2: (" + p2.getX() + ", " + p2.getY() + ")");
System.out.println("Distance between p1 and (0, 0): " + p1.distance(0, 0));
System.out.println("Distance between p1 and p2: " + p1.distance(p2));
Point2D midpoint = p1.midpoint(p2);
System.out.println("Midpoint between p1 and p2: (" + midpoint.getX() + ", " + midpoint.getY() + ")");
System.out.println("Distance between p1 and p2 :" + Point2D.distance(p1, p2));
}
}