Why do we need PACKAGES in java?

BOXIn our previous set of tutorials, you must have noticed the use of package and you have seen the import statements following the package statement in most of the classes. Yes, by now I expect you to have been familiar with these two keywords since I have briefly discussed them here but you probably don’t understand why we needed the packages, created the different packages in my last series of posts, put some classes within them and why I had to make call to the Import statement. Whatever the case, this post is to clear your doubts, if any, and make you understand why packages are important.

First, when you think about a package, what comes to your mind? For me, I think of a wrapped box with related Items – remember when you are checking out at the mall, the attendant packs related items together. For example, food items are not put close to detergents. Packages in Java does something similar to that of the mall attendant. It brings together related classes and interface into the same package so as to avoid mix up. For the sake of definition, a package is a collection of related classes and interfaces. To have a clearer view, let us assume families A, B, C and D with two children each with the profession shown in the illustration below:

Continue reading

Advertisements

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

JAVA OPERATOR II

As promised in my last post, I will be sharing sample codes showing how Java operators behave. I have named the Java classes in the sample codes after the operator class and I also added the result of each operation on the variables.

Sample Class showing Arithmetic Operations:

arithmeticOperators

Continue reading

And the journey began…

Yaay!!!I finally made it. I am a female Java programmer who got to learn the hard way or what do you call learning on the job? Imagine how it feels to be expected to deliver on something you have little or no expertise or clue on…yeah, that was how I felt January 2014 when i joined the bank and ended up in the software development team. I really can’t blame anybody for ending up there because I remember personally going to my boss’s office requesting to be posted to the team. This was after an intensive or should I say grueling four months training in everything about banking but not technology!…Mehn!

Continue reading