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

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…

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..

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

What really is Object Oriented Programming?

Over the last couple of weeks, we have looked at Java Modifiers and Basic Java Keywords and syntax I and II. As you may already know, Java is an object oriented program (OOP). OOP (Object Oriented Programming) as a programming model involves the collection of interacting objects. An Object is a black box which contains code and data, it sends and receives messages and are used to represent real life concepts ranging from human beings (described by name, address, and so forth) to countries (whose properties can be described and managed) down to the little widgets on a computer desktop (such as buttons and scroll bars). Some of the concepts of OOP have been discussed in my earlier posts however, despite these discussions  OOP will be incomplete without explaining the following concepts:

Continue reading