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

Note: You don’t have to write yours exactly as I have written mine but I hope you get the whole idea behind it.

Now that we have seen how the interface has been defined and implemented in both PolygonController and NonPolygonController classes, the next thing is to consider the particular shape in question. I have defined the shapes as different classes in com.bee.code.blog.MoreShapes package in this tutorial for ease of understanding and to avoid unnecessary confusion. Each shape will set the needed parameter to calculate its area and call up the calculateAreaOfShapes method on the related implementation class (PolygonController or NonPolygonController) depending on the type of the shape. Now let us take the shapes one by one.

The following steps were used by the shape classes.(Using the Parrallelogram.java class below as example )

  1. Create an object of the parameter class. (Line 14)
  2. Set the required parameter for calculation of the area having in my the formula for the area of the shape. (Line 15 & 16)
  3. Create an object of the required implementing class while making reference to the interface. (Line 18)
  4. Call the calculateAreaOfShapes method of the implementing class and pass the parameter object to it. (Line 19)
  5. Return the output of the calculation. (Line 20)

Knowing that the area of a parallelogram is base multiplied by height, we know the parameters required for calculating the area are base and height. (1) An object of this the parameter class is created, (2) the base and height parameter are set, (3) an object of polygonController is created (since parallelogram falls under polygon) with reference to the interface, (4) calculateAreaOfShapes method is called and the object parameters are passed into the method together with the type of shape(result passed to output variable), (5) a value is returned after calculation(output) which is returned to the user.

Parrallelogram.java – Area=bh

 1 package com.bee.code.blog.MoreShapes;
 2 
 3 import com.bee.code.blog.ShapeCommon.ShapeVariableBean;
 4 import com.bee.code.blog.ShapeController.AllShapesInterface;
 5 import com.bee.code.blog.ShapeController.PolygonController;
 6 
 7 /**
 8  *
 9  * @author 'beecodeblog
10  */
11 public class Parrallelogram{
12 
13     public double doArea(double b, double h) {
14         ShapeVariableBean parralParam = new ShapeVariableBean();
15         parralParam.setBase(b); 
16         parralParam.setHeight(h);
17         
18         AllShapesInterface service=new PolygonController();
19         double output = service.calculateAreaOfShapes(parralParam,"PARRALLELOGRAM");
20         return output;
21     }
22 }

The same steps apply to the other shapes as shown in their code snippets below:

Rectangle.java – Area =lb

 1 package com.bee.code.blog.MoreShapes;
 2 
 3 import com.bee.code.blog.ShapeCommon.ShapeVariableBean;
 4 import com.bee.code.blog.ShapeController.AllShapesInterface;
 5 import com.bee.code.blog.ShapeController.PolygonController;
 6 
 7 /**
 8  *
 9  * @author beecodeblog
10  */
11 public class Rectangle  {
12 
13     public double doArea(double b, double h) {
14         ShapeVariableBean rectParam = new ShapeVariableBean();
15         rectParam.setBase(b);
16         rectParam.setHeight(h);
17         AllShapesInterface service=new PolygonController();
18         return service.calculateAreaOfShapes(rectParam,"RECTANGLE");
19        
20     }
21 }
22

Square.java –Area= l*l

 1 package com.bee.code.blog.MoreShapes;
 2 
 3 import com.bee.code.blog.ShapeCommon.ShapeVariableBean;
 4 import com.bee.code.blog.ShapeController.AllShapesInterface;
 5 import com.bee.code.blog.ShapeController.PolygonController;
 6 
 7 /**
 8  *
 9  * @author 'beecodeblog
10  */
11 public class Square  {
12 
13     public double doArea(double l) {
14         ShapeVariableBean squareParam = new ShapeVariableBean();
15         squareParam.setLength(l);
16         AllShapesInterface service=new PolygonController();
17         return service.calculateAreaOfShapes(squareParam,"SQUARE");
18        
19     }
20 }
21

Triangle.java –Area= (1/2)bh

 1 package com.bee.code.blog.MoreShapes;
 2 
 3 import com.bee.code.blog.ShapeCommon.ShapeVariableBean;
 4 import com.bee.code.blog.ShapeController.AllShapesInterface;
 5 import com.bee.code.blog.ShapeController.PolygonController;
 6 
 7 /**
 8  *
 9  * @author 'beecodeblog
10  */
11 public class Triangle {
12 
13     public double doArea(double b, double h) {
14         ShapeVariableBean triParam = new ShapeVariableBean();
15         triParam.setBase(b);
16         triParam.setHeight(h);
17         AllShapesInterface service=new PolygonController();
18         return service.calculateAreaOfShapes(triParam,"TRIANGLE");
19        
20     }
21 }
22

RegularPolygon.java – Area = 1/2 (perimeter * apothem)

 1 package com.bee.code.blog.MoreShapes;
 2 
 3 import com.bee.code.blog.ShapeCommon.ShapeVariableBean;
 4 import com.bee.code.blog.ShapeController.AllShapesInterface;
 5 import com.bee.code.blog.ShapeController.PolygonController;
 6 
 7 /**
 8  *
 9  * @author beecodeblog
10  */
11 public class RegularPolygon {
12 
13     public double doArea(double a, double p) {
14         ShapeVariableBean polyParam = new ShapeVariableBean();
15         polyParam.setApothem(a);
16         polyParam.setPerimeter(p);
17         AllShapesInterface service=new PolygonController();
18         return service.calculateAreaOfShapes(polyParam,"OTHERS");
19        
20     }
21 }
22

Summary: Today, we have been able to discuss how to separate concerns and to be more specific, we created object of a class, set the necessary parameters, called up a method on an object, passed parameters to methods and returned output. In my next post, I will be concluding this tutorial by showing the interaction with users of this application. In addition, you will learn how to receive the required parameters from users, process the request and display output to the user. Until then, happy coding!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s