37 38 39 40 41 42 43 44 45 public double getResult() { return performTheOperation() ; //6} /** Perform the operation…..generate the result*/ private double performTheOperation( ) { double aResult = 0.0 ; 46 47 48 if ( operation.equals(”+”) ) aResult = operand1 + operand2 ; else 49 50 51 if ( operation.equals(”-”) ) aResult = operand1 - operand2 ; else 52 53 54 if ( operation.equals(”*”) ) aResult = operand1 * operand2 ; else 55 56 57 if ( operation.equals(”/”) ) aResult = operand1 / operand2 ; 58 59 60 } } return aResult ; A cursory examination of Listing 6-1 reveals its purpose, which is to serve as a simple calculator. CalcBean has four properties: two operands, an operation, and the result of the operation applied to the operands. To keep things simple, this bean does not include any code that checks for errors (such as zerodivide) or performs any edit checks on property values. Line 1 shows the bean stored as a package. It is a good Java programming practice to use packages to group related classes together. Here, we have only one class so the use of packages may seem a bit pedantic. Later, you ll see where you should put the CalcBean class within the Tomcat server s directory structure. Line //2 shows the declarations for the bean properties. Notice that the properties are declared with the privatevisibility modifier. The bean requirement is that bean properties are not declared public; you can have declared bean properties as protected or use default package visibility. If our bean has subclasses that need access to the bean s properties, you can make a case for using the protected visibility modifier. Of course, you can still declare the properties private and use get/set methods within the subclasses to gain access to the bean properties. Note that because bean properties are permitted to have initialized values, when this bean is created, its properties are given these initial values. Line //3 shows the no-arg constructor. You do not need to explicitly code the empty constructor because, as you recall, Java uses the empty constructor by default. The important point is not to code a constructor that requires arguments. Line //4 shows a typical set method and line //5 shows a typical get method. The coding of the get and set methods follows the convention described in the preceding section. Remember, if you do not code your accessor methods according to the described convention, bean tools and the JSP container recognize the methods as accessor methods. Line //6 shows a get method that invokes a method to generate the result of the calculation. You could have included the body of the method performTheOperation within the get method or coded performTheOperationin another bean or class. JavaBeans, as software components, must have the ability to communicate with other components. In the following section, you can read how to code your beans so they can communicate with other beans.
We feature a web hosting shopping cart and live support solution. Just try our web hosting shopping cartwhich provides a secure way of obtaining payments through your website.