BRINGING THE PIECES TOGETHER IV – User Interaction

Today we will be concluding the set of posts I have been sharing on how to bring the pieces of our previous lessons together and write a meaningful code. We will focus on User Interaction but before we go ahead, let us have a recap of all we have done so far.

  • Setting up of IDEs,
  • Creation of packages, classes, methods, variables and identifies,
  • Brief overview of access modifiers
  • Declaration of constructors
  • Use of Java statements: If-Else and return statements
  • Implementation of an interface – declaration of an interface, addition of abstract methods to an interface and implementation of an interface.
  • Working with objects by creating instance of a class,
  • Encapsulation – making use of setters and getters and we were able to set parameters and get some parameters using this concept.
  • Calling up methods from implementing classes of an interface by creating object of the implementing class with reference to the interface

Continue reading

Advertisements

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