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

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.

Continue reading

More on Object Oriented Programming…

In my previous post, I started discussion on Object Oriented Programming click here if you missed it where I explained Encapsulation and Inheritance as OOP concepts. This post will be wrapping up this lesson with Abstraction and Polymorphism concepts.

Abstraction:This is a process whereby important details of a method are hidden and only the functionality is shown to the user. For instance, Abstraction will only tell the user things like a class can cook, wash, and fly by listing out the functions but not how the actual cooking, washing and flying are done. In Java, the ‘what a class can do’ is known as method while the ‘how the method does them’ is called Implementation. Therefore we can simply say abstraction list methods and hides implementation.

Continue reading