BRINGING THE PIECES TOGETHER II – Interface Declaration and Implementation

In my last post, we started bringing together the pieces of our previous tutorials together – we were able to set up our IDEs, created packages, classes, variables and Identifiers. In this post, we will continue from where we stopped which was the ShapeVariableBean.java class. Looking again at the structure of our application (AreaofShapes) as we discussed earlier here, we have three packages, nine (9) classes and one (1) interface altogether as shown below.

ProjectHeirachy2

In the last lesson, I explained the purpose of each package and we saw the content of ShapeVariableBean and how encapsulation came to play. Today, we will focus more on the com.bee.code.blog.ShapeController package and also, we will see how other classes implements interface within this package. In com.bee.code.blog.ShapeController package, the shapes are divided into two; PolygonController and NonpolygonController.

Note: As an update on the last post, I added a new item on this package which is what we will be focusing on today. I added interface AllShapesInterface above so we can see how to make use of an interface in the course of these set of lessons. Before we continue, please refer to my earlier post on interface where I explained in detail what an Interface is in case you missed the lesson.

AllShapesInterface.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 interface AllShapesInterface {
10     
11  public double calculateAreaOfShapes(ShapeVariableBean param, String type);
12 
13 }

AllShapesInterface.java

An interface is known to contain a collection of abstract methods. AllShapesInterface contains an abstract method as shown in the code snippet above, we have our sample interface that contains abstract method calculateAreaOfShapes. The interface listed out the function (i.e. what the interface is providing) but did not describe how this function will be achieved. We use Interfaces in situations when we know that some task must be done, but how it should be done varies depending on what class is using it. For instance, we know we need to calculate the area of different shapes but the method for calculating area of the different shape differs from one shape to another. Hence we defined a method calculateAreaOfShape in our interface to be implemented by the different shapes.

Now, let us move to the implementation of this interface.

Polygon controller class – manages all shapes that fall under polygon (a plane figure with at least three straight sides and angles, and typically five or more). They are triangles, rectangles, pentagons, hexagons and so on. The polygon controller contains implementation methods for different types of polygons as seen below

PolygonController.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 PolygonController implements AllShapesInterface {
10 
11     public PolygonController() {
12     }
13     
14 
15     @Override
16     public double calculateAreaOfShapes(ShapeVariableBean param, String type) {
17         double area = 0.0;
18         if (type.equalsIgnoreCase("TRIANGLE")) {
19             area = calculateAreaOfTriangle(param);
20         } else if (type.equalsIgnoreCase("SQUARE")) {
21             area = calculateAreaOfSquare(param);
22         } else if (type.equalsIgnoreCase("RECTANGLE")) {
23             area = calculateAreaOfRectangle(param);
24         } else if (type.equalsIgnoreCase("PARRALLELOGRAM")) {
25             area = calculateAreaOfParralelogram(param);
26         } else {
27             area = calculateAreaOfOtherPolygon(param);
28         }
29         return area;
30     }
31 
32 // Area of a triangle =1/2(bh)
33     private double calculateAreaOfTriangle(ShapeVariableBean param) {
34         double result = 0.0;
35         if (param.getBase() != 0.0 && param.getHeight() != 0.0) {
36             result = (0.5) * (param.getBase() * param.getHeight());
37         }
38         return result;
39     }
40 
41     // Area of a square =l*l
42     private double calculateAreaOfSquare(ShapeVariableBean param) {
43         double result = 0.0;
44         if (param.getLength() != 0.0) {
45             result = (param.getLength() * param.getLength());
46         }
47         return result;
48     }
49 
50 // Area of a rectangle =lb
51     private double calculateAreaOfRectangle(ShapeVariableBean param) {
52         double result = 0.0;
53         if (param.getLength() != 0.0 && param.getBreadth() != 0.0) {
54             result = (param.getLength() * param.getBreadth());
55         }
56         return result;
57     }
58 
59 // Area of a parralelogram =bh
60     private double calculateAreaOfParralelogram(ShapeVariableBean param) {
61         double result = 0.0;
62         if (param.getBase() != 0.0 && param.getHeight() != 0.0) {
63             result = (param.getBase() * param.getHeight());
64         }
65         return result;
66     }
67 
68 //Area of a regular polygon= 1/2 (perimeter * apothem)
69     private double calculateAreaOfOtherPolygon(ShapeVariableBean param) {
70         double result = 0.0;
71         if (param.getApothem() != 0.0 && param.getPerimeter() != 0.0) {
72             result = (0.5) * (param.getApothem() * param.getPerimeter());
73         }
74         return result;
75     }
76 
77 }
78 

If we have a close look at the code above, we will see that I have made use of the If-Else statement. Refer to my earlier post on this in case you missed my post on If and Else statement. The If-Else statement is used in this case to select a method to call so as to perform the calculation of the area of the shape as shown on lines 19, 21, 23, 25 and 27 above. Therefore, any time we are trying to calculate the area of a triangle, square, rectangle, parallelogram and other regular polygon, methods calculateAreaOfTriangle, calculateAreaOfSquare, calculateAreaOfRectangle, calculateAreaOfParrallelogram,  calculateAreaOfOtherPolygon will be called and  executed respectively. I have defined the different methods as private since they are only accessed directly from this class, see my earlier post on accessmodifiers in case you missed it and see above the implementation of the methods using specific formula for the type of shape on lines 33, 42, 51, 60 and 69.

So we have seen how to declare an interface, add an abstract method to it and implement the abstract method. Also we have seen in the course of this lesson, the use of If-Else statement and access modifier. One more thing to note is the constructor, someone might have probably being wondering why I highlighted a part of the code red (line 11 and 12), and this is to lay emphasis on the Constructor.

NOTE: The code above is good with or without this constructor definition. This is because in Java, there is a default constructor for every class. The default constructor is  non-parameterized i.e. it doesn’t accept any arguments just as the one above. However, in a situation where a constructor other than the one above (i.e a constructor with a parameter) has been defined, the default constructor will not be generated hence it becomes necessary to also define the default constructor explicitly if you will need to refer to it. Please refer to my earlier post on constructor here.

To continue with the implementation of the interface, we can use the NonPolygon Controller and define implementation for Nonpolygon shapes such as circle and sphere. If you like, you can go ahead and implement this before my next post, I will share a sample code of how I have implemented mine before we continue next week. Please feel free to reach out through comment or direct email should you have any challenge while going through this tutorial. Happy coding!!!

Advertisements

2 thoughts on “BRINGING THE PIECES TOGETHER II – Interface Declaration and Implementation

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