Thursday, May 31, 2018

Java Concepts[1]-Classes and Objects

Hi, I’m going to explain important basic java concepts in an easy way using simple and real-life examples.

First I’m going to explain you about java classes and objects.

Class is a user defined blueprint/template that describes the behavior/state the object of its type.

Object is an instance of a class. It consists of states and behavior. 

We’ll take Human class
States ➝ Variables/Fields
Behaviors   ➝    Methods


Following is an example of how to code the above scenario in Java.


#tip 1- Class name should be a noun/nouns and the first letter should be upper case. If there is more than one noun, first letter of each word should be capitalized. (ex. Human, TwoWheelVehicle)

#tip 2- Method name should be a verb and should start with a lower case letter. When there is more than one, the first letter should be lower case with the first letter of internal words capitalized. (Ex-eat, getName)

#tip 3-Variable names should start with a lower case letter. (name, firstName)
           
           There are 3 types of variables.
           1.Local Variables- Variables that declared inside the method. These variables can be used only within that method.

                                                 

            2.Instance Variables-Variable that declared inside a class but outside any method. These variables are initialized when the class is instantiated. These variables can be accessed from inside any method, constructor or blocks of that particular class.



           3.class variables- Variables declared inside a class but outside any method with the static keyword. There is only one copy of this variable that is shared with all instances of that class.

                               


Illustration on instance variables and class variables
'noHumans' is a static variable. So it is stored inside the Class memory and common to all objects. 'name', 'age' and 'weight' are instance variables and they are stored inside the object memory. So each object will have its own copy of instance variables




#tip 4- Every class has a constructor. Constructors should have the same name as the class. A class can have more than one constructor


I hope you understood the classes and objects of Java clearly. If you need more clarification on anything please feel free to leave a comment

Thank You.

Sahani Perera

Wednesday, May 30, 2018

Code Java with NetBeans

Are you a beginner to NetBeans?

 Here is a quick guide for you to start a java project with NetBeans.

1.First Start the NetBeans IDE.

  This is the start page you will get when you start the IDE.

In this it will show the recent projects. As this is your first project, you will get nothing here.



2.Then go to Files  New Project




 3. Then you will get New Project wizard. In that you have to select the category and projects. You need to select them as follows.

Then Click next.


4. Then in this step you need to give the name of your application and the location where you are going to locate it.
             (For my project I gave the name as ‘Example’. You can give a name as your desire and you can browse and select the location you want. )

Project Folder and create main class field will change accordingly. Leave the rest as follows.


Then click Finish.


Then you will get the editor. Beside that you will get the project window and it will contain a tree view of components of the project including source files, libraries etc.

And there is navigator window, which you can use to quickly navigate between elements within the selected class.




Now you are ready to add your code to the project.

Let’s see how to add the code and run the project.

We'll add a simple code to print Hello World.








             Then we need to save, compile and run the project. 

             To save ---> go to file at the top left corner and select save.



No need to do the compilation separately. We only need to click the run button.

  The Green Color Triangle shape button is the run button




Then you will get the output as follows.



If you get the output as above,  Congratulation your code is working.

Now it is time for you to add more codes and run the program.

Sunday, May 22, 2016

Tail Recursion

Tail Recursion
Recursion means defining something in terms of itself. When we take a problem we can divide it into different parts or sub problems. To solve each of those sub problems we can call the functions. In recursion, inside the function we use the same function over and over again.
         The tail recursion is a special case of recursion.In this there is no computations after the recursive call. In the function the recursive call should be the final instruction.The recursive call is not compose with reference to the memory cells. In normal recursion, the computer has to remember the return address but in tail recursion it does not need to do so. After the recursive call it does not have to any performance, so it immediately return the calling function. Because of that tail recursion improves the efficiency. The following examples will explain the above explanations clearly.

Normal recursion 
def multiple(n):
       if n==1:
                 return 1
       else:
                 return n*multiple(n-1)
print(multiple(4))

This will execute as follows.
4*(multiple(3))
4*(3*multiple(2))
4*(3*(2*multiple(1)))
4*(3*(2*(1)))
24
 From the above example you can clearly see that after calling the recursive call it has done some calculations.

Tail recursion
def multiples(n,rec=1):
        if n==1:
                return rec
        else:
                return multiples(n-1, rec*n)
print(multiples(4,1))

This will execute as follows.
multiples(4,1)
multiples(3,1*4)
multiples(2,4*3)
multiples(1,12*2)
24

In this way also it gives the same answer.After the recursive call it had not done any calculations , it had simply deliver the answer we needed.


The benefits of tail recursion are efficiency , readability, etc