-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculateDistances.java
More file actions
95 lines (79 loc) · 2.56 KB
/
CalculateDistances.java
File metadata and controls
95 lines (79 loc) · 2.56 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package problems.codechef;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/**
* Created by Arpit on 12-Dec-15.
*/
public class CalculateDistances {
private double pointx1;
private double pointy1;
private double pointx2;
private double pointy2;
private double distance;
public double getPointx1() {
return pointx1;
}
public void setPointx1(double pointx1) {
this.pointx1 = pointx1;
}
public double getPointy1() {
return pointy1;
}
public void setPointy1(double pointy1) {
this.pointy1 = pointy1;
}
public double getPointx2() {
return pointx2;
}
public void setPointx2(double pointx2) {
this.pointx2 = pointx2;
}
public double getPointy2() {
return pointy2;
}
public void setPointy2(double pointy2) {
this.pointy2 = pointy2;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double x1, y1, x2, y2, distance, x2minusx1, y2minusy1;
ArrayList<CalculateDistances> listOfPoints = new ArrayList<CalculateDistances>();
// we want to find the distance in 10 cases.
for (int i = 0; i < 5; i++) {
// "example of input: 2 3 4 3" It means: point1(2,3) & point2(4,3)
String line = scan.nextLine();/*.trim();*/
String[] points = line.split("\\s");
System.out.println(Arrays.toString(points));
x1 = Double.parseDouble(points[0]);
y1 = Double.parseDouble(points[1]);
x2 = Double.parseDouble(points[2]);
y2 = Double.parseDouble(points[3]);
CalculateDistances obj = new CalculateDistances();
obj.setPointx1(x1);
obj.setPointy1(y1);
obj.setPointx2(x2);
obj.setPointy2(y2);
listOfPoints.add(obj);
}
scan.close();
/*
* Distance Formula: Given the two points (x1, y1) and (x2, y2), the
* distance between these points is given by the formula:
* squareRoot((x2-x1)^2+(y2-y1)^2)
*/
for (CalculateDistances cd : listOfPoints) {
x2minusx1 = cd.getPointx1() - cd.getPointx2();
y2minusy1 = cd.getPointy1() - cd.getPointy2();
distance = Math.sqrt(Math.pow(x2minusx1, 2)
+ Math.pow(y2minusy1, 2));
System.out.println((int)distance);
}
}
}