forked from LaunchCodeEducation/java-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.java
More file actions
60 lines (43 loc) · 1.16 KB
/
Copy pathPoint.java
File metadata and controls
60 lines (43 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.launchcode.java.demos;
/**
* Created by LaunchCode
*/
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double computeDistanceFromOrigin() {
return Math.sqrt((x*x + y*y));
}
public Point computeMidpoint(Point p) {
double midpointX = (x + p.getX()) / 2;
double midpointY = (y + p.getY()) / 2;
return new Point(midpointX, midpointY);
}
public String toString() {
return "x=" + x + ", y=" + y;
}
public static void main(String[] args) {
Point p1 = new Point(3,3);
System.out.println(p1.toString());
Point p2 = new Point(-2, 4);
System.out.println(p2);
System.out.println("p1 distance from origin: " + p1.computeDistanceFromOrigin());
System.out.println("midpoint of p1 and p2: " + p1.computeMidpoint(p2));
}
}