Bringing The Pieces Together…

Recently, one of my BCB readers asked me a question that brought about this blog topic, he said “I have read through your several posts but how do I bring them all together and write a complete functional code?” Then I thought to myself that he was right, probably other readers might also have the same challenge bringing all the pieces together. Therefore, in this post and my next couple of posts, we will be taking a deep dive, referring back at every topic on Java we have previously dealt with on this blog in order to write a complete functional code. I hope you are ready to get your hands really dirty…lol.

Assumption: To start with, I assume that by now you have installed Java and you have java path set properly on your machine. If not, please refer to my previous post on this Lay your Bed this Christmas. Remember to  direct all questions to me should you have any challenge following the steps.  I will be using Netbeans IDE (Integrated Development Environment) for this example, you can use any IDE you are comfortable  with to run this example. See list of possible IDEs and where to download them on this post Lay your Bed this Christmas.

Task: We will be writing a sample program to calculate the area of different basic shapes i.e. rectangle, square, circle etc..

Solution Algorithm: For starters, the first thing is to think about the problem and map out a solution in form of an algorithm

  1. Begin
  2. Enter the type of shape
  3. Enter required parameters e.g. length, breadth etc. for the shape selected
  4. Calculate the area of shape
  5. Output result
  6. End

Development Steps

  1. Launch the Netbeans IDE, or your preferred IDE, and create a new project AreaOfShapes. In case you are not sure on how to go about that, please follow the Create an IDE Project steps on this link and if you are using a Text Editor, refer to my previous post on this Happy New Year in JAVA!!!
  2. Now that we have created our project in step 1 above, let us refer back to my post on Basic Java Keywords and Syntax I and focus on packages and classes.


So this is how my project structure looks, I have created my packages and classes as shown in the image above.

Packages:  From the example above, I have created three packages as listed below

  1. : Within this package, I created two classes which contains the main method notice the play symbol (Main method is the first method that is called when a class runs) and to manage the common variables needed by the shapes.
  2. ShapeController : Within this package, I have categorized the shapes into two; polygons and non-polygons
  3. : Within this package, I have been more explicit in defining the type of shape whose area is being calculated. Hence, we have Parallelogram, Rectangle, Square, Triangle and other regular polygons

Do this: Create the packages and classes shown in the picture above following the steps below:

  • Right click on the project > Click on New >
  • On the drop down list, select “Java Classes” and a New Java Class Screen will pop up.
  • Enter the appropriate class name and package name
  • Click on the “Finish” button when you are done.
  • Go through the same steps until you have completely created the Classes and Packages above.


  1. Still on Basic Java Keywords and syntax, let us see how to make use of this lesson to work with variables and identifiers. Remember that a variable can hold data ranging from texts(String), numbers(int,long), decimal numbers(double, float) and temporary results of multi step calculations etc. I used double in this example because it is possible for our variable value to be a decimal and I also used double over float because of its  larger capacity .

  1 package;
  3 /**
  4  *
  5  * @author 'beecodeblog
  6  */
  7 public class ShapeVariableBean {
  9     public static final double PIE_VALUE = 3.142;
 10     private double length;
 11     private double breadth;
 12     private double base;
 13     private double height;
 14     private double radius;
 15     private double apothem;
 16     private double perimeter;
 18     /**
 19      * @return the length
 20      */
 21     public double getLength() {
 22         return length;
 23     }
 25     /**
 26      * @param length the length to set
 27      */
 28     public void setLength(double length) {
 29         this.length = length;
 30     }
 32     /**
 33      * @return the breadth
 34      */
 35     public double getBreadth() {
 36         return breadth;
 37     }
 39     /**
 40      * @param breadth the breadth to set
 41      */
 42     public void setBreadth(double breadth) {
 43         this.breadth = breadth;
 44     }
 46     /**
 47      * @return the base
 48      */
 49     public double getBase() {
 50         return base;
 51     }
 53     /**
 54      * @param base the base to set
 55      */
 56     public void setBase(double base) {
 57         this.base = base;
 58     }
 60     /**
 61      * @return the height
 62      */
 63     public double getHeight() {
 64         return height;
 65     }
 67     /**
 68      * @param height the height to set
 69      */
 70     public void setHeight(double height) {
 71         this.height = height;
 72     }
 74     /**
 75      * @return the radius
 76      */
 77     public double getRadius() {
 78         return radius;
 79     }
 81     /**
 82      * @param radius the radius to set
 83      */
 84     public void setRadius(double radius) {
 85         this.radius = radius;
 86     }
 88     /**
 89      * @return the apothem
 90      */
 91     public double getApothem() {
 92         return apothem;
 93     }
 95     /**
 96      * @param apothem the apothem to set
 97      */
 98     public void setApothem(double apothem) {
 99         this.apothem = apothem;
100     }
102     /**
103      * @return the perimeter
104      */
105     public double getPerimeter() {
106         return perimeter;
107     }
109     /**
110      * @param perimeter the perimeter to set
111      */
112     public void setPerimeter(double perimeter) {
113         this.perimeter = perimeter;
114     }
116 }

In the code snippet above, I have declared eight (8) variables(line 9  to 16) with variable names (identifiers) PIE_VALUE, length, breadth, base, height, radius, apothem, and perimeter respectively. Remember from my post Basic Java Keywords and syntax II that there are two types of variables; the static and non-static variable – In the example above, “PIE_VALUE” is a static variable as it was defined with the static keyword hence the reason why it is appropriate to be used for pie since its value is not specific to any instance but is common to all instances of the class and cannot be altered.

One more thing to note in the example above is that we have used one of the OOP concepts (Encapsulation), refer to my previous post on “What really is Object Oriented Programming? We have declared the variables as private and generated setters and getters to allow calling class to modify and access the values of the variables indirectly.

In summary, we have been able to set up our IDE, create packages, classes, variables and Identifiers and we have also seen how to make use of these keywords in writing a functional code. Please endeavour to replicate what has been discussed here and feel free to ask questions should you require any help while following the tutorial. This tutorial will be continued from here in my next post…stay tuned!



2 thoughts on “Bringing The Pieces Together…

Leave a Reply

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

You are commenting using your 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