AssignmentS2
This commit is contained in:
BIN
assignments/AssigmentS2/exercise1/Point2D.class
Normal file
BIN
assignments/AssigmentS2/exercise1/Point2D.class
Normal file
Binary file not shown.
37
assignments/AssigmentS2/exercise1/Point2D.java
Normal file
37
assignments/AssigmentS2/exercise1/Point2D.java
Normal 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);
|
||||
}
|
||||
}
|
18
assignments/AssigmentS2/exercise1/demo.java
Normal file
18
assignments/AssigmentS2/exercise1/demo.java
Normal 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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user