BRINGING THE PIECES TOGETHER III – working with Objects

In my last post, we focused on declaration of interface and implementation of an interface method. We were able to create interface AllShapesInterface and we implemented the method interface in PolygonController class where we also defined the methods to calculate the areas of various shapes. I asked you to do the same for NonPolygonController class, I hope you were able to do this? As promised, the sample code of my own implementation is shown below:

NonPolygonController.java

 1 package com.bee.code.blog.ShapeController;
 2 
 3 import com.bee.code.blog.ShapeCommon.ShapeVariableBean;
 4 
 5 /**
 6  *
 7  * @author 'beecodeblog
 8  */
 9 public class NonPolygonController implements AllShapesInterface {
10 
11     public NonPolygonController() {
12     }
13 
14     @Override
15     public double calculateAreaOfShapes(ShapeVariableBean param, String type) {
16         double area = 0.0;
17         if (type.equalsIgnoreCase("CIRCLE")) {
18             area = calculateAreaOfCircle(param);
19         } else if (type.equalsIgnoreCase("SPHERE")) {
20             area = calculateAreaOfSphere(param);
21         } else {
22             System.out.println("Sorry, shape not found!!!");
23         }
24 
25         return area;
26     }
27 
28     // Area of circle= pie *radius *radius
29     private double calculateAreaOfCircle(ShapeVariableBean param) {
30         double result = 0.0;
31         if (param.getRadius() != 0.0) {
32             result = ShapeVariableBean.PIE_VALUE * param.getRadius() * param.getRadius();
33         }
34         return result;
35     }
36 
37     //Area of a sphere= 4 * pie *radius * radius
38     private double calculateAreaOfSphere(ShapeVariableBean param) {
39         double result = 0.0;
40         if (param.getRadius() != 0.0) {
41             result = 4 * ShapeVariableBean.PIE_VALUE * param.getRadius() * param.getRadius();
42         }
43         return result;
44     }
45 }
46

Continue reading