-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherObservation.java
More file actions
309 lines (281 loc) · 10.3 KB
/
Copy pathWeatherObservation.java
File metadata and controls
309 lines (281 loc) · 10.3 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* class WeatherObservation stores information about an individual monthly weather observation
*
* @author (Chris)
* @version (a version number or a date)
*/
public class WeatherObservation
{
public static final double missingData=Double.NEGATIVE_INFINITY;
private int year;
private int month;
private double tMax;
private double tMin;
private double dayOfFrost;
private double tRain;
private double tSunshine;
private boolean provisional;
/**
* Constructor for objects of class WeatherObservation
*
* @param y New value for the WeatherObservation's year.
* @param m New value for the WeatherObservation's month.
*/
public WeatherObservation(int y, int m) throws InvalidDataException
{
year=y;
month=m;
tMax = missingData;
tMin = missingData;
dayOfFrost= missingData;
tRain = missingData;
tSunshine = missingData;
provisional = false;
}
/**
* Constructor for objects of WeatherObservation
*
* @param s New value for the observation .
*
*/
public WeatherObservation(String s) throws InvalidDataException
{
String[] splitString = s.trim().split("[\\s*#]+");
try {
this.year = Integer.parseInt (splitString[0]);
setMonth (Integer.parseInt (splitString[1]));
this.tMax = missingData;
this.tMin = missingData;
this.dayOfFrost = missingData;
this.tRain = missingData;
this.tSunshine = missingData;
this.provisional = false;
if (splitString[2].equals ("---") == false) {
setTMax (Double.parseDouble (splitString[2]));
}
if (splitString[3].equals ("---") == false) {
setTMin (Double.parseDouble (splitString[3]));
}
if (splitString[4].equals ("---") == false) {
setDayOfFrost (Integer.parseInt (splitString[4]));
}
if (splitString[5].equals ("---") == false) {
setTRain (Double.parseDouble (splitString[5]));
}
if (splitString[6].equals ("---") == false) {
setTSunshine (Double.parseDouble (splitString[6]));
}
} catch (InvalidDataException e) {
throw new InvalidDataException ("Data is not complete");
} catch (NumberFormatException e) {
throw new InvalidDataException ("Digital number format error.");
}
this.provisional = (splitString.length > 7 && splitString[7].equals ("Provisional")) ? true : false;
}
/**
* A get method - year of the observation.
*
* @return The current value of the observation's year.
*/
public int getYear(){
return year;
}
/**
* A get method - month of the observation.
*
* @return The current value of the observation's month.
*/
public int getMonth(){
return month;
}
/**
* A get method - mean daily maximum temperature in celsius of the observation.
*
* @return The current value of the observation's mean daily maximum temperature(e.g.6.3) in celsius).
*/
public double getTMax(){
return tMax;
}
/**
* A get method - mean daily minimum temperature in celsius of the observation.
*
* @return The current value of the observation's mean daily minimum temperature(e.g.0.7) in celsius).
*/
public double getTMin(){
return tMin;
}
/**
* A get method - number of days of air frost in mm of the observation.
*
* @return The current value of the observation's number of days of air frost(e.g. 258.2) in mm.
*/
public double getDayOfFrost(){
return dayOfFrost;
}
/**
* A get method - the observation's total rainfall in mm of the observation.
*
* @return The current value of the observation's total rainfall(e.g. 258.2) in mm).
*/
public double getTRain(){
return tRain;
}
/**
* A get method - the observation's total hours of sunshine of the observation.
*
* @return The current value of the observation's total hours of sunshine(e.g.42.0)).
*/
public double getTSunshine(){
return tSunshine;
}
/**
* A set method - year of the observation.
*
* @param year New value for the observation's year.
*/
public void setYear(int year){
this.year=year;
}
/**
* A set method - month of the observation.
*
* @param month New value for the observation's month.
*/
public void setMonth(int month) throws InvalidDataException {
if ( month > 12 || month < 1) {
throw new InvalidDataException ("Month is not vailed");
}
this.month=month;
}
/**
* A set method - mean daily maximum temperature in celsius of the observation.
*
* @param tMax New value for the observation's mean daily maximum temperature in celsius.
*/
public void setTMax(double tMax)throws InvalidDataException{
if ( tMax < -273.15 ) {
throw new InvalidDataException ("Mean daily maximum temperature must > -273.15 (absolute zero)");
}
this.tMax=tMax;
}
/**
* A set method - mean daily minimum temperature in celsius of the observation.
*
* @param tMin New value for the observation's mean daily minimum temperature in celsius.
*/
public void setTMin(double tMin)throws InvalidDataException{
if ( tMin < -273.15 ) {
throw new InvalidDataException ("Mean daily minimum temperature must > -273.15 (absolute zero)");
}
this.tMin=tMin;
}
/**
* A set method - number of days of air frost in mm of the observation.
*
* @param day New value for the observation's number of days of air frost in mm.
*/
public void setDayOfFrost(double day) throws InvalidDataException{
if (day < 0) {
throw new InvalidDataException ("Number of days of air frost must > 0");
}
dayOfFrost=day;
}
/**
* A set method - the observation's total rainfall in mm of the observation.
*
* @param tRain New value for the observation's the observation's total rainfall in mm.
*/
public void setTRain(double tRain)throws InvalidDataException{
if (tRain < 0) {
throw new InvalidDataException ("Total hours of sunshine must > 0");
}
this.tRain=tRain;
}
/**
* A set method - mean daily minimum temperature in celsius of the observation.
*
* @param tSunshine New value for the observation's mean daily minimum temperature in celsius.
*/
public void setTSunshine(double tSunshine)throws InvalidDataException {
this.tSunshine=tSunshine;
}
/**
* A set method - to indicate whether or not the observation is provisional.
*
* @param isP New value for indicating whether or not the observation is provisional .
*/
public void setProvisional (boolean isP)
{
provisional = isP;
}
/**
* A get method - to find the monthly mean temperature, calculated from the average of the mean daily maximum and mean daily munimum temperature.
*
* @return The current value of the observation's monthly mean temperature.
*/
public double getMMTemp(){
double monthlymtemp= Math.round((tMax+tMin)/2);
return monthlymtemp;
}
/**
* A toSting method- returns a string representation that formats the data in exactly the same way taht the MetOffice files record the data.
*
* @return A string representation that formats the data in exactly the same way taht the MetOffice files record the data.
*/
public String toString(){
String strTMax, strTMin, strForst, strRain, strSunshine;
String NullString = " --- ";
strTMax = (this.tMax == missingData) ? NullString : String.format ("%8.1f", tMax);
strTMin = (this.tMin == missingData) ? NullString : String.format ("%8.1f", tMin);
strForst = (this. dayOfFrost== missingData) ? NullString : String.format ("%8d", dayOfFrost);
strRain = (this.tRain == missingData) ? NullString : String.format ("%8.1f", tRain);
strSunshine = (this.tSunshine== missingData) ? NullString : String.format ("%8.1f", tSunshine);
return String.format ("%4d %2d", this.year, this.month) + strTMax + strTMin + strForst + strRain + strSunshine;
}
/**
* A completion method - to find data completion percentage of the obsevation.
*
* @return The current value of the observation's data completion percentage.
*/
public double completion ()
{
double valid = 0;
valid = (this.tMax == missingData) ? valid : valid + 1;
valid = (this.tMin == missingData) ? valid : valid + 1;
valid = (this.dayOfFrost == missingData) ? valid : valid + 1;
valid = (this.tRain == missingData) ? valid : valid + 1;
valid = (this.tSunshine == missingData) ? valid : valid + 1;
return (valid / 5) * 100;
}
/**
* A euqals method - The method determines whether the Number object that invokes the method is equal to the object that is passed as argument.
*
* @return The methods returns True if the argument is not null and is an object of the same type and with the same numeric value.
*/
public boolean equals (Object o)
{
boolean same = false;
if (o != null && o instanceof WeatherObservation)
{
same = ((this.year == ((WeatherObservation) o).year) && (this.month == ((WeatherObservation) o).month));
}
return same;
}
/**
* A compareTo method - The method compares the Number object that invoked the method to the argument.
*
* @return If the Integer is equal to the argument then 0 is returned.
* @return If the Integer is less than the argument then -1 is returned.
* @return If the Integer is greater than the argument then 1 is returned.
*/
public int compareTo (WeatherObservation o)
{
if (year == o.getYear()) {
if (month == o.getMonth()) {
return 0;
}
return (month > o.getMonth()) ? 1 : -1;
}
return (year > o.getYear()) ? 1 : -1;
}
}