5 private String soilName;

6 private double soilDepth;

7 public SoilO {}

8 public double getWaterStress(){

9 double waterStress = 0.0;

10 //here goes the body of the method, to be implemented

11 return waterStress;

Figure 3-3. Java definition of class Soil.

Line 1 in Figure 3-3 shows the package or the subdirectory where the Java code is stored. The concept of package will be introduced later in this book. Line 3 shows that a new class, referred to as Soil, is defined. Lines 5 and 6 show the definition of attributes for class Soil. Attribute soilName is of type String; the values this attribute can hold should be of type String. Attribute soilDepth is of type double, the values this attribute holds should be of type double. Line 7 describes the default constructor for class Soil. A constructor is the mechanism in Java that creates instances of a class. Lines 8 through 12 are the definition of the operation get WaterStress. Line 8 shows that the result of the operation is of type double. Line 9 defines a local attribute named waterStress of type double. This attribute will hold the calculated value of water stress parameter. Line 10 is a comment in Java that shows that the logic for water stress calculation needs to be provided by the user. Line 11 returns the value of the calculated parameter to the object that asked for it.

The number of attributes and operations that a class is provided with directly affects the behavior of the class. Designing the attributes and the operations of a class is not an easy task. It has to do with the role and the responsibilities the class will have in the domain in study. It is through the attributes and the operations that the responsibilities of a class are carried out.

0 0

Post a comment