-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCity.java
87 lines (77 loc) · 2.06 KB
/
City.java
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
import java.util.*;
/**
* Class for a city
*/
public class City {
private final double RRR = 6378.388;
private final double pi = 3.1415926;
private String name;
private int locationNum;
private List<Location> coordinates;
private double[][] distances;
/**
* Constructor
* @param name of the city
* @param locationNum number of locations in the city
*/
public City(String name, int locationNum) {
this.name = name;
this.locationNum = locationNum;
this.coordinates = new ArrayList<>();
this.distances = new double[this.locationNum][this.locationNum];
}
/**
* Getter
* @return city name
*/
public String getName() {
return this.name;
}
/**
* Getter
* @return number of locations
*/
public int getNum() {
return this.locationNum;
}
/**
* Getter
* @return distances between locations
*/
public double[][] getDistances() {
return this.distances;
}
/**
* Setter
* @param name of the city
*/
public void setName(String name) {
this.name = name;
}
/**
* Setter
* @param number of the location
* @param x coordinate
* @param y coordinate
*/
public void setCoordinate(int number, double x, double y) {
Location temp = new Location(number, x, y);
this.coordinates.add(temp);
}
/**
* Calculate the distances between any two locations
*/
public void setDistance() {
for (int i = 0;i < this.locationNum;i++) {
for (int j = i+1;j < this.locationNum;j++) {
double x1 = this.coordinates.get(i).getX();
double y1 = this.coordinates.get(i).getY();
double x2 = this.coordinates.get(j).getX();
double y2 = this.coordinates.get(j).getY();
double dis = Math.sqrt(Math.pow((x1-x2),2) + Math.pow((y1-y2),2));
this.distances[i][j] = dis;
this.distances[j][i] = dis;
}
}
}
}