-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem09.java
More file actions
48 lines (46 loc) · 1.34 KB
/
Problem09.java
File metadata and controls
48 lines (46 loc) · 1.34 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
class Point{
private double[] point;
public Point(double[] p) {
this.point = p;
}
public double[] getPoint() {
return this.point;
}
}
class EuclideanDistance{
static double getDist(Point p1, Point p2) {
if(p1.getPoint().length == p2.getPoint().length) {
double sum = 0;
for(int i=0;i<p1.getPoint().length;i++)
sum+= Math.pow(p1.getPoint()[i]-p2.getPoint()[i],2);
return Math.sqrt(sum);
}
else
return -1.0;
}
}
class ManhattanDistance{
static double getDist(Point p1, Point p2) {
if(p1.getPoint().length == p2.getPoint().length) {
double sum = 0;
for(int i=0;i<p1.getPoint().length;i++)
sum += Math.abs(p1.getPoint()[i]-p2.getPoint()[i]);
return sum;
}
else
return -1.0;
}
}
public class Problem09 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Point p1 = new Point(new double[] {1.0,2.0,3.0});
Point p2 = new Point(new double[] {4.0,5.0,6.0});
System.out.println("Euclidean Distance: " + EuclideanDistance.getDist(p1, p2));
System.out.println("Manhattan Distance: " + ManhattanDistance.getDist(p1, p2));
Point p3 = new Point(new double[] {1.0,2.0,3.0});
Point p4 = new Point(new double[] {4.0,5.0});
System.out.println("Euclidean Distance: " + EuclideanDistance.getDist(p3, p4));
System.out.println("Manhattan Distance: " + ManhattanDistance.getDist(p3, p4));
}
}