Developing the Calculator Session Bean

Use this procedure to create a session enterprise bean that calculates the result from two float numbers. The enterprise bean has four business methods for adding, subtracting, dividing, and multiplying the numbers.


You have created the EJB module project CalculatorEjb. The structure of your project is currently displayed in the J2EE Explorer.


Specifying the general properties of the Session Bean


       1.      In the J2EE Explorer, select the project node CalculatorEjb and open the context menu. Choose New ® EJB…

       2.      In the New EJB wizard screen that appears, assign the following values to the properties of the new EJB.



EJB Name


EJB Project


Bean Type

Stateless Session Bean

Default Package

Generate default interfaces


       3.      Choose Next.

The New EJB wizard appears and displays the screen containing names of the remote and the local interfaces.

       4.      Accept the suggested names of the local and remote interfaces and choose Next.

In the screen that appears, you can add the required business methods.

Creating business methods

In the Calculator bean, you need to add and implement the four business methods: add, subtract, multiply, and divide. For all of these methods, you will specify float as the return value and two parameters f1 and f2, each of them also of type float.

       5.      To add the first method add,select the Business Methods folder in the left pane of the wizard screen and choose Add.

       6.      Enter the name add and assign the return type float.

       7.      Add the parameters f1 and f2 of type float to this method.

       8.      In the left pane of the wizard screen, select the Business Methods folder to update the display.

       9.      To add the other methods (subtract,  multiply, and divide), repeat the last steps as appropriate.

   10.      Choose Finish to create the Session Bean.

In the J2EE Explorer, a new substructure named CalculatorBean is inserted automatically under the project node CalculatorEjb ® ejb-jar.xml.

Implementing the business methods

   11.      In the project structure, expand the CalculatorEjb ® ejb-jar.xml node and double-click the name CalculatorBean.

   12.      In the overview that appears, choose Navigate to Bean Class.

The Java Editor opens and displays the source code of the generated bean class.

   13.      Navigate to the implementation of the business methods.

   14.      Modify the return values as follows:


    * Business Method.


   public float add(float f1, float f2) {

       return f1 + f2;




    * Business Method.


   public float subtract(float f1, float f2) {

       return f1 - f2;




    * Business Method.


   public float multiply(float f1, float f2) {

       return f1 * f2;




    * Business Method.


   public float divide(float f1, float f2) {

       return f1 / f2;


   15.      Save the contents of the editor by choosing the appropriate icon from the toolbar.

The Developer Studio updates and compiles the project sources.


The Calculator bean has been created in package. The package now contains the bean’s class and the bean’s home and remote interfaces. You have also completely implemented the business methods of the Session Bean.

Next step:

You continue with creating a Enterprise Bean archive.