-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLatLong.java
More file actions
134 lines (121 loc) · 3.47 KB
/
Copy pathLatLong.java
File metadata and controls
134 lines (121 loc) · 3.47 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* class LatLong represents a geographical position, based on latitude and longitude.
*
* @author (Chris)
* @version (11/11/2015)
*/
public class LatLong
{
private int lat;
private int lon;
/**
* Constructor for objects of class LatLong
*
* @param lon New value for the position's longitude.
* @param lat New value for the position's latitude.
*/
public LatLong(int lon, int lat)
{
this.lat=lat;
this.lon=lon;
}
/**
* Constructor for objects of class LatLong
*
* @param position New value for the position.
* The format must be "####E ####N" (# is a decimal number) i.e. in the form 1234E 5678N.
*/
public LatLong(String position) throws InvalidDataException{
try{
String[] pArray= position.split("\\s+");
String slat= pArray[1].substring(0,pArray[1].length()-1);
String slon= pArray[0].substring(0,pArray[0].length()-1);
String sllat= pArray[1].substring(pArray[1].length()-1,pArray[1].length());
String sllon= pArray[0].substring(pArray[0].length()-1,pArray[0].length());
if(sllon.equals("E"))
{
this.lon= Integer.parseInt(slon);
}
else if(sllon.equals("W"))
{
this.lon= -Integer.parseInt(slon);
}
else
{
throw new InvalidDataException("LatLong : Longitude format error.");
}
if (sllat.equals("N"))
{
this.lat=Integer.parseInt(slat);
}
else if(sllat.equals("S")){
this.lat=- Integer.parseInt(slat);
}
else{
throw new InvalidDataException("LatLong : Latitude format error.");
}
}
catch(StringIndexOutOfBoundsException e)
{
throw new InvalidDataException("LatLong : String format error.") ;
}
catch(NumberFormatException e)
{
throw new InvalidDataException("LatLong : Digital number format error.") ;
}
}
/**
* A set method - Latitude of the position.
*
* @param lat New value for this position's latitude.
*/
public void setLat(int lat)
{
this.lat=lat;
}
/**
* A set method - Longitude of the position.
*
* @param lon New value for the position's longitude.
*/
public void setLon(int lon)
{
this.lon=lon;
}
/**
* A get method - Latitude of the position.
*
* @return The current value of the position's latitude.
*/
public double getLat()
{
return lat;
}
/**
* A get method - Longitude of the position.
*
* @return The current value of the position's longitude.
*/
public double getlon()
{
return lon;
}
/**
* A toString method- returns a string representation of the LatLong's position.
*
* @return A string representation of the LatLong's position in the UK grid system. i.e. in the form 1234E 5678N
*/
public String toString()
{
int ilat;
int ilon;
String slat;
String slon;
ilat= (Math.abs(this.lat));
ilon= (Math.abs(this.lon));
slat=(this.lat > 0) ? "N" : "S";
slon=(this.lon > 0) ? "E" : "W";
System.out.println(ilat + slat + " " + ilon + slon);
return ilat+slat+""+ilon+slon;
}
}