Info

26 System.out.println("Cannot connect to the weather station");

27 System.exit(l);

Figure 8-37. Definition of class WeatherDataFromStation in Java (Part 1 of 5).

Lines 1 through 3 import the libraries needed for implementing the behavior of the class. Notice that in addition to the libraries needed in the definition of the class WeatherDataFromFile, there is another one referred to as java.net. * that provides behavior to communicate with the Internet. Line 5 defines the class WeatherDataFromStation that implements behavior defined in interface IWeather. Lines 6 through 10 define the attributes of the class. Line 12 defines the constructor of the class. Line 14 defines the method initialize(Properties props). The property file props holds the parameters needed to identify the weather station. The parameters are the weather station number, the starting and ending date for the time interval used in the simulation. Lines 16 through 18 read the above mentioned parameters from the property file. Lines 19 through 23 create an instance of type URL using a parameter that provides information about the address of the server and the SQL1 statement to be executed by the server providing the data. Lines 26 and 27 show that in the case the system cannot connect to the weather station, an error message is displayed and the execution halts.

Lines 29 through 35 read the first line of data from the server and set to 0 the attribute dayOfYear. The address of the server is: http://fawn.ifas.ufl.edu /scripts/fawndataserver.asp and the SQL statement is:

SELECT AirTempMin,AirTempMax,Rainfall,TotalRad,ET FROM dailysummary WHERE id=stationNumber

AND datetime>= startingDate AND datetime<endingDate . Note that some additional characters are needed to fill the empty spaces in the SQL statement in Java language. The rest of the code is shown as follows.

30 buffReader = new BufferedReader(new

Input StreamReader(url. openStr eam()));

31 String firstLine = (String)buffReader.readLine(); //eliminates the titles 32}

33 catch (IOException e){e.printStackTrace();}

34 setDayOfY ear(0); 35}

3 6 public boolean hasNext() {

38 return buffReader.ready();

40 catch (IOException e) {

41 return false;

44 public Object next() {

46 String line = (String)buffReader.readLine();

47 StringTokenizer tokens = new StringTokenizer(line,",,f);

49 String [] dailyData = new String [tokens.countTokens()];

50 dailyData[0] = tokens. nextToken();

51 dailyDatafl] = tokens.nextToken();

52 dailyData[2] = tokens.nextToken();

53 daily Data[3] = tokens.nextToken();

54 currentDay=new DailyWeatherData( dailyDatafO], dailyData[l],dailyData[2],dailyData[3]);

57 catch (IOException e){}

5 8 setDay OfYear(getDayOfYear()+1);

59 return currentDay;

Figure 8-37. Definition of class WeatherDataFromStation in Java (Part 3 of 5)

Note that the Java code for classes WeatherDataFromFile and WeatherDataFromStation are similar. The difference is the part of the code that identifies the source of the data. In the case of class WeatherDataFromFile, special code is needed to read the data from the file whereas in the case of class WeatherDataFromStation, special code is needed to establish connection with the weather station. In both cases, method initialize(Properties props) is used to connect with the data source.

61 public void setDayOfYear(int dayOfYear) {

62 this.dayOfY ear=dayOfY ear;

64 public int getDayOfYear() {

65 return dayOfY ear;

67 public double getAverageTemperature() {

68 return currentDay.getAverageTemperatureQ;

71 public double getSolarRadiation() {

72 return currentDay.getSolarRadiation();

74 public double getAverageTempDuringDay() {

75 return currentDay.getAverageTemperature();

77 public double getAverageTemperatureForPT() {

78 return currentDay.getAverageTemperatureForPT();

80 public double getRainFall() {

81 return currentDay.getRainFall();

83 public double getTemperatureMin() {

84 return currentDay.getTemperatureMin();

86 public double getTemperatureMax() {

87 return currentDay.getTemperatureMax();

89 public double getPAR() {

90 return currentDay.getPAR();

92 public double getPotentialET() {

93 return currentDay.getPotentialET();

0 0

Post a comment