Info

10 public void initialize(Properties props) {

12 String flleName = props.getProperty(f,weatherFile");

13 FileReader fileReader = new FileReader(fileName);

14 br = new BufferedReader(fileReader);

15 setDayOfY ear(0);

17 catch (FileNotFoundException e)

19 System, out.printing Weather file not found; the system will shut down");

20 System, exit(l); // Implementation of the precondition

22 catch (IOException e){ System, out.println("IO Exception");}

24 public boolean hasNext() {

26 return br.ready();

28 catch (IOException e){return false;}

30 public Object nextQ {

Lines 5 through 7 define the attributes of class WeatherDataFromFile. Note that line 7 defines an attribute of type Daily WeatherData; its implementation details were presented in Figure 8-34. Line 8 defines the class constructor; in this case, it is a default constructor.

Lines 10 through 23 define the body of the method initialize(Properties props). The scope of this method is to prepare the environment for obtaining the weather data. An instance of the class FileReader is created using the current value of the attribute filename read from the property file. The property file is made available to object Weather by the simulator controller. In the case that the weather file is not found, the system halts the execution and displays an error message, as shown in lines 19 and 20. These lines (19 and 20) show the implementation of the precondition for the use case as mentioned in Section 2.4, Preconditions, in this chapter. Then, an instance of class BufferReader is created using the file reader already obtained and the day of the year is set to zero. The input-output library imported in line 1 provides the functionality required to read data from a text file. Lines 24 through 29 show the implementation of method hasNextQ defined in the interface Iterator. This method will return the result true when data are available and false otherwise. Lines 30 through 44 define the method nextQ that provides the next set of available weather data. Line 32 reads a line from the data container. Line 33 divides the entire line into tokens. Tokens are created by considering the values separated by comma.

32 String line = (String)br.readLine();

33 StringTokenizer tokens = new StringTokenizer(line,",M);

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

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

37 dailyData[l] = tokens.nextToken();

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

39 dailyData[3] = tokens.nextToken();

40 dailyData[4] = tokens.nextToken();

41 currentDay=new Daily WeatherData(dailyData[ 1 ], dailyData[2], dailyData[3], dailyData[4]);

44 catch (IOException e){System.out.println("Error reading data");}

Line 34 makes sure that the string read in line 32 contains real data. Line 35 creates an array with size the number of tokens created in line 33. Lines 36 through 40 assign to an element of an array a token that represents a value (rainfall, for example) from the daily data set. Line 41 creates an instance of the class Daily WeatherData with the tokens obtained previously. The values of tokens will be assigned to the attributes of class Daily WeatherData. Line 44 shows an exception that may occur while reading the data from the text file. It is a good programming practice to provide users with the right information when an exception occurs during the execution of the program. The user is informed about the cause of the exception and then, can decide what decision to make next.

45 setDayOfY ear(getDayOfY ear()+1);

46 return currentDay;

49 public void setDayOfYear(int dayOfYear) {

50 this. dayOfY ear=dayOfY ear;

52 public int getDayOfYear() {

53 return dayOfY ear;

55 public Daily WeatherData getDailyDataQ {

56 return currentDay;

58 public double getSolarRadiation() {

59 return currentDay.getSolarRadiation();

Figure 8-35. Definition of class WeatherDataFromFile in Java (Part 3 of 4).

Line 45 increases by one the number of days since the beginning of the simulation. Line 46 returns an instance of the class Daily WeatherData populated with current weather data read from the file. The order of the weather data in the file is the following: Solar radiation, temperature max, temperature min, and rainfall.

Line 48 is the definition of the method removeQ; this method does not have any body, as it is not used. We are obliged to provide an empty implementation for this method as it is part of the interface Iterator. Class WeatherDataFromFile implements interface Iterator and therefore, an implementation for each of the methods of the interface is needed to be part of the class definition. An empty implementation for a method means that the method does not provide any functionality. Lines 49 through 51 define the method that can change the value of attribute dayOfYear. This method uses the parameter dayOfYear to substitute the existing value of the attribute. Lines 52 through 54 define a method that returns the value of the attribute dayOfYear. Lines 55 through 57 define the method getDailyDataQ that returns an instance of class DailyData already populated with weather data for a specific day.

61 public double getTemperatureMax() {

62 return currentDay.getTemperatureMax();

64 public double getTemperatureMin() {

65 return currentDay.getTemperatureMin();

67 public double getRainFall() {

68 return currentDay.getRainFall();

70 public double getPAR() {

71 return currentDay.getPAR();

73 public double getAverageTempDuringDay() {

74 return currentDay.getAverageTempDuringDay();

76 public double getAverageTemperatureForPT() {

77 return currentDay.getAverageTemperatureForPT();

79 public double getAverageTemperature() {

80 return currentDay.getAverageTemperature();

Figure 8-35. Definition of class WeatherDataFromFile in Java (Part 4 of 4).

Lines 58 through 82 define methods that allow other objects to access specific weather data stored in the current instance of Daily WeatherData. Note that these methods use the Delegation pattern, defined in the first part of the book in Section 2.1 of Chapter 7. As class WeatherDatafromFile implements interface IWeather, it should provide an implementation of all of the methods defined in the interface. Data such as rainfall or solar radiation are stored in an instance of class Daily WeatherData. WeatherDataFromFile does not have access to these individual data, but it has access to the instance of DailyWeatherData that holds them and therefore it delegates the method call to this instance. The format of the file holding the weather data is shown in Figure 8-36. The first column shows that it is the first day of the year 1987. The next columns hold the data for temperature minimum, temperature maximum, solar radiation, and rainfall. In our implementation of the Kraalingen approach we have used a slightly different approach to obtain the day of the year. Instead of extracting it from the Day Off ear value, we have defined a new attribute named dayOfYear that initially is set to zero and is increased by one at each step of the simulation.

DayofYear Tmin Tmax Radiation Rainfall

87001,

5.1,

20,

4.4,

0 0

Post a comment