15 public double getSolarRadiation() {

16 return solarRadiation;

Figure 8-34. The implementation of class DailyWeatherData in Java (Part 1 of 2).

Lines 3 through 6 define the attributes of the class. Note that these attributes are defined as private; they cannot be accessed outside the class definition by using the attribute name. Lines 8 through 13 define the class constructor, the Java mechanism for creating instances of a class. Lines 15 through 26 define methods to access values attributes hold. Lines 27 through

39 define additional methods for providing other types of daily weather data obtained by manipulating the core daily data.

18 public double getTemperatureMax() {

19 return maxTemperature;

21 public double getTemperatureMin() {

22 return minTemperature;

24 public double getRainFall() {

25 return rainFall;

27 public double getPAR() {

28 return 0.50*getSolarRadiation();

30 public double getAverageTempDuringDay() {

31 return 0.6*getTemperatureMax()-f0.4*getTemperatureMin();

33 public double getAverageTemperatureForPT() {

34 return

0.25* getTemperatureMin()+0.75* getTemp eratureMax();

36 public double getAverageTemperature() {

37 return

0.5* getTemperatureMin()+0.5 * getTemperatureMax();

Figure 8-34. The implementation of class DailyWeatherData in Java (Part 2 of 2).

As shown in Figure 8-34, once an instance of the class DailyWeatherData is created, individual data can be obtained using its accessor methods. As an example, the method getTemperatureMinQ sent to this object will return the value of the minimum temperature. Note that the definition of this class does not depend on the container used for holding the weather data.

Figure 8-35 shows the implementation in Java for class WeatherDataFromFile. The role of this class in the simulation process is to provide weather data stored locally in a text file. Lines 1 and 2 show the libraries that need to be imported for the class definition. These are the input-output and utility libraries. The utility library contains the Properties utility, used in line 12. Line 3 defines the class WeatherDataFromFile, stating that this class implements the interface IWeather. Because IWeather inherits from interface Iterator, class WeatherDataFromFile needs to implement behavior defined in both interfaces; in addition to the behavior defined in IWeather interface, this class should also implement the behavior defined in the Iterator interface. Iterator is an interface defined in the java.utillterator library that is provided by the Java development environment. The same Java environment provides java. io. * library.

3 public class WeatherDataFromFile implements IWeather {

0 0

Post a comment