Thursday, 29 August 2013

Comments in Programming


What is Comments and how it is use?

Comments is the Essential element of all programming language.And It is use mostly all top most languages like

  • C
  • C++
  • C#
  • JAVA
  • Python
  • PHP
  • Perl


Comments are use to describe the logic ,methodology and for remembering  the code working.
Every Programmer must use these comments.Actually this is use in each and every programming language.
Let consider ,if you write some programming code and after 1 month you see those code then its very hard to understand the logic behind the code.

There are different type of comments

Single line Comment  //
Multi Line Comment  /*   */

Complier leave the comment and  understand that this is a human code not for me



Example


int number;   //This is declaration


Now compiler only work on "int number;" and leave the remaining part of this line



Similarly case with multi line comments


int number =4;

number++;
/*Initialization the integer number
and increment the number value 
by 1 value;
*/



Simple Calculator in Console

Simple Calculator In JAVA
This Program simple take values and operator of computation and give the output.

import java.util.*;

class Calculator{
public static void main(String []args){
int n1,n2,ans;   //Declaration
char op;  // Declaration

Scanner input=new Scanner(System.in);
System.out.println("Enter 1st Number ");
n1=input.nextInt();

System.out.println("Enter Operation  ");
op=input.next();

System.out.println("Enter 2nd Number ");
n2=input.nextInt();

if(op=='+')
ans=n1+n2;

else if(op=='-')
ans=n1-n2;

else if(op=='*')
ans=n1*n2;

else
ans=n1/n2;


System.out.println("Answer = "+ans);
}
}

INPUT
2
+
2

OUTPUT
4

Wednesday, 28 August 2013

Method/Function in JAVA

What is Function in Programming??

If someone ask you to write same 10 line like

This is a Java Programming Tutorial and you get great concept from here.
This is a Java Programming Tutorial and you get great concept from here.
This is a Java Programming Tutorial and you get great concept from here.
This is a Java Programming Tutorial and you get great concept from here.
This is a Java Programming Tutorial and you get great concept from here.
This is a Java Programming Tutorial and you get great concept from here.
This is a Java Programming Tutorial and you get great concept from here.
This is a Java Programming Tutorial and you get great concept from here.
This is a Java Programming Tutorial and you get great concept from here.
This is a Java Programming Tutorial and you get great concept from here.
This is a Java Programming Tutorial and you get great concept from here.
This is a Java Programming Tutorial and you get great concept from here.


Then your feeling become bored ,but just imagine what is your feeling when you just call a single method to perform
all above work.That is a Function that make programming easier and reduce complexity.Similarly you see in MATHEMATICS there are function which give some output with each input.

Now come on actual defination of function/method

"Function is a Collection of statement that perform an task".
Example
System.out.println() is a method : use to output the screen at console


Syntax



accessModifer returntype functionName(Parameter)
{
function body
}

Example
public void show()
{
System.out.println("This is a Java Programming Tutorial and you get great concept from here.");
}

Tuesday, 27 August 2013

String function in JAVA

There are some important built in function of String variable


import java.util.*;

class Program
{
public static void main(String []args)
{
String value="YASIR SHABBIR";
String value1="Hello";
System.out.println(value.trim()); //Show String without whitespace
System.out.println(value..subString(2,4));  // Show the SubString the String value
System.out.println("Hello : "+value);  //   Concatenate the the String variable
System.out.println(value1+value); //   Concatenate the the String variable
System.out.println(value(charAt(2))); //Show the character at parameter position
if(value.equals("YASIR SHABBIR"))
System.out.println("Yes matach the result");


System.out.println(value.toUpperCase());  //Give the result in Upper Case
System.out.println(value.toLowerCase());  //Give the result in Lower case
System.out.println(value.length());  //Give the length of String


}
}

Input Value By user at runtime

JAVA Program that take value user at run time mean at the time of console interaction.

import java.util.Scanner;

class Program
{
public static void main(String []args)
{
Scanner input=new Scanner(System.in);
int number=input.nextInt();
float fNumber=input.nextFloat();
String value=input.nextLine(); //get value string at console

System.out.println(number);  //Print the integer value
System.out.println(fNumber); //Print the Float Number
System.out.println(value); //Print the String value
}
}



INPUT
4
5.5
yasir

OUTPUT
4
5.5
yasir



What is Console?
This answer  only for those who don't know about the word console or Shell and I  only give the answer in pictorial



Bubble Sort Function In JAVA

BUBBLE SORT

This is a sorting algorithm which arrange the elements of Array in to descending or ascending type.

public static void bubbleSort(int [] array)

{
int temp;
for(int i=0;i<array.length;i++)
{
for(int j=0;j<array.length-1;i++)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}


If you want to sort array in descending order then

if(array[j]<array[j+1])
{
temp=array[j];
array[j+1]=array[j];
array[j]=temp;
}

Variable and Datatype In Java

Variable mean varying value.Variable carry small amount of portion of memory and its value can be change during throughout program execution.Variable Name are the actually name of the memory unit.
Consider variable is a basket which carry something and something is a value.
Variable can be store integers value,character value, floating value, yes/no value ; these are called datatype of variable.





Datatype are the type of data can be storing in the variable.There are 8 type of datatype

  1. Boolean (Yes/No) (1 Bit of Memory)
  2. byte (Small Integer Value) (1 Byte of Memory)
  3. short( Small but larger integer than byte value) (2 Byte of Memory)
  4. int (Integer Value : +1,-1) (4 Byte of Memory)
  5. long (Large Integer Value) (8 Byte of Memory)
  6. float(Floating value ) (4 Byte of Memory)
  7. double(Large Floating value) (8 Byte of Memory)
  8. char (Character Value) (2 Byte of Memory)


Syntax of Using Variable with Datatype

DataType variableName;
variableName=value;

Example

int number;
number=12;


Binary Search Method of Java Code


public static void binarySearch(int []Array,int value,int start,int end)
{
int middle=0;
while(true)
{
        middle=(start+end)/2;
        if( start>end){
        System.out.println("Not found");
        break;}
        else if(Array[middle]>value){
        start=middle;}
        else{
        end=middle;}
}

Adding 2 Number using variable

class Program
{
public static void main(String []args)
{
int number1=2;
int number2=2;
int number3=number+number2;
System.out.println(number3);
}
}

OUTPUT
4

int number1=2;    : This line declare and give the value to integer variable.
int number2=2;    : This line declare and give the value to integer variable.
int number3=number+number2;  : This line also declare and give value to integer variable using previous variable.

System.out.println(number3);   : This give the output on console of variable (number3) that have contain value..

Java Program of Adding 2 Number

This is simple program that add 2 integer number and print the result

class Program
{
public static void main(String []args)
{
System.out.println(2+2);
}
}

OUTPUT
4

System.out.println(2+2) : this line just output the result 4.

Monday, 26 August 2013

First Program of JAVA

Today we will learn first program of JAVA.

 class HelloWorld{
public static void main(String [] args){
System.out.println("Hello World");
}
}

First line : "class" is a keyword that function is to declare class,"HelloWorld" is the Class name .

2nd Line : It is the starting point of program . public ,static ,void and String are the keyword ;eachone have their own functionality.
public is declare something to public-ally  accessible.It actually a access modifier.Access Modifier is also apart topic.
"static" mean declare something all for equally,for example if any one want to share the today Date then all have same value.
"void" mean nothing or  it return nothing .When Function portion of programming will discuss then you get good idea about this word 'void' also about the Function topic.
"(String []args)" it is a simple parameter to get value at compile time.

"{  }" : this is the boundary of   class HelloWorld.

3rd Line: System.out.println("HelloWorld"); : It just simple give the output of HelloWorld at the console at the runtime.If you want to write something at console then type between " ".

The all discussion we have discussed include in all programming portion.So therefore it is very important for beginner to understand thoroughly .Now we include this in all .
If you have any query related this ,then comment me below. :)

Sunday, 25 August 2013

What is Polymorphism in OOP

Polymorphism is the essential pillar of Object Oriented Programming ,it come to use after encapsulation and Inheritance.
Polymorphism is a Greek word that means "many-shaped" : One thing can perform different work of each same call. Polymorphism enables us to "programming in the general" rather than "program in specific.
Let consider that example that commonly use in programming tutorial for example a method/function calculate the area, if any one can want to calculate the area of triangle then it give the output ,if any person want to get the area of Parallelogram then it give the area of this similarly if any one want to get area of circle then it give result ....
Now as you seen in above example that same thing can perform many action



This Picture give the abstract idea of Polymorphism.
There are many other terminology  in Polymorphism but here I am not discuss because I would not want to confuse you , but when we will on practical programming we discuss and tell you clear way.
Now I Hope you enjoy the tutorial. Please leave comment if you any Question ... :)
  

Saturday, 24 August 2013

What is Abstraction in OOP

Abstraction (from the Latin abs, meaning away from).
Abstraction is the feature of OOPS in which we show the essential features of an object and suppress the unnecessary details. Personally I think  abstraction cannot achieved without Encapsulation, before this you should have the concept of Encapsulation.


Lets understand by daily routine example
Suppose we have created a Bank class and its having one of the methods called getInterest() which will return the interest of particular user. Now whenever we would like to know the interest of particular account holder will simple call this methods, we are not interested how the interest is calculated + what all operations or formulas its using and all we are simple interested in the result that is to [show only the necessary details without including the background details.

By Definition "Abstraction is the way to do work without knowing how is this happening"
it is the headache of method to perform corresponding task.





Just see this picture if want to fire the bullet then we simple pressed  trigger now its the headache of trigger to fire the bullet.
I hope you understand the idea of Abstraction... :) 
 

What is Inheritance

Inheritance is the major and usable principal of OOP.
here just we see and understand the concept of inheritance ..Further clarification in practical programming.So lets try to understand it.

As the name inheritance suggests an object is able to inherit characteristics from another object.
By example we can see the real world example like
Let's say we make a class called "Human" that represents our physical characteristics. It's a generic class that could represent you, me or anyone in the world. Its state keeps track of things like number of legs, number of arms, and blood type. It has behaviors like eat, sleep, and walk. Human is good for getting an overall sense of what makes us all the same but it can't for instance tell me about gender differences. For that we'd need to make two new class types called "Man" and "Woman". The state and behaviors of these two classes will differ from each other in a lot of ways except for the ones that their inherit from Human.
Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class.

Here is the method and variable of class in diagram ,like animal can "EAT" this is the method or action that they can perform of any type mammal.

What is Encapsulation in Java



Encapsulation is major principal of Object Oriented Programming.

Encapsulation is the word which came from the word "CAPSULE" which to put some thing in a kind of shell. In OOP we are capsuling our code not in a shell but in "Objects" & "Classes".  So it means to hide the information into objects and classes is called Encapsulation.





Encapsulation is actually a way of coding in which public methods and private variables are bind into wrapper(Class)  so that Object of that Class can have consistent and correct value.No have have the direct access of class attribute to modify it.Here we Encapsulation can by Information Hiding through private attribute and (Getter ,Setter) method of class.Encapsulation give your Object into a consistent and correct state.
Encapsulation is a way of organizing data and methods into a structure by concealing the the way the object is implemented, i.e. preventing access to data by any means other than those specified. Encapsulation therefore guarantees the integrity of the data contained in the object.


Here is an example of Encapsulation

    public class Employee{
         private String name;
         
         public String getName(){
             return this.name;
         }

         public void setName(String name){
                this.name=name;
         }

    }

In above the Employee class have single private attribute called name and this can be only accessible from outside the class through these getter(getName) and setter(setName).


Wednesday, 21 August 2013

OOP

Stands for "Object-Oriented Programming." OOP (not Oops!) refers to a programming methodology based on objects, instead of just functions and procedures.
Object-oriented programming makes it easier for programmers to structure and organize software programs. Because individual objects can be modified without affecting other aspects of the program, it is also easier to update and change programs written in object-oriented languages. As software programs have grown larger over the years, OOP has made developing these large programs more manageable.
JAVA is also a pure 99.99% Object Oriented Programming Language.
There are also a many hundreds language that based on OOP;list are following below


  • C++
  • C#
  • Delphi
  • PHP
  • Python
  • Visual Basic
There are 4 major principle of OOP
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction
these are core concept of OOP ,if any say that he is a Programmer and don't know the these principal concept then he is Liar and fake telling with you... 

Lets try to understand the idea of OOP by real world story:


To help you understand objects and their contents,let’s begin with a simple analogy. Suppose you want to drive a car and make it go faster by pressing its accelerator pedal. What must happen before you can do this? Well, before you can drive a car, someone has to design it.
A car typically begins as engineering drawings, similar to the blue prints that describe the design of a house (this is Classes in OOP).When we implement car using this map or blue print then this is called Object. Car can do many more function or action like start the engine turn on the AC etc etc( these are Method/Function in classes).
Every car inherit or have he functionality of previous model( This Concept is Inheritance). These drawings include the design for an accelerator pedal. The pedal hides from the driver the complex mechanisms that actually make the car go faster, just as the brake pedal hides the mechanisms that slow the car, and the steering wheel “hides” the mechanisms that turn the car. This enables people with little or no knowledge of how engines, braking and steering mechanisms work to drive a car easily(this is Encapsulation and data abstraction concept of OOP).


Each and Every class have 2 thing in it
1) Method/Function
2)Attribute /Data Object.



We hope to understand the ideal logy of OOP but when we will learning new chapter of programming then all the concept that we have already mentioned  
help you to make further more strong.


Tuesday, 20 August 2013

Compilation and Execution Process of JAVA Program














First see the whole picture carefully each and every thing then read further.


First see the picture and Only thing that are unfamiliar with you are "Byte Code"
What is Byte Code???
The Java compiler translates your Java program into a language called byte code. This byte code is not the machine language for any particular computer, but it is similar to the machine language of most common 
computers. Byte code is easily translated into the machine language of a given computer. Each type of computer will have its own translator called an interpreter—that translates from byte code instructions to 
machine-language instructions for that computer.

Why is it called Byte code?
Programs in low-level languages, such as byte code and machine-language code, consist of instructions, each of which can be stored in a few bytes of memory. Typically, one byte of each instruction contains the operation code, or op code, which specifies the operation to be performed. The notion of a one-byte op code gave rise to the term byte code.

Algorithm Introduction

Basically Algorithm is the main course of computer science field ,thousand of books on this; but now we
can try to explain the word of "ALGORITHM" so lets start it

An algorithm is a finite, step-by-step procedure for accomplishing some task or
solving a problem.
Algorithms are everywhere. Every time you query Google, a Web-mining algorithm runs;
every time you use Mapquest for directions, a shortest-path algorithm runs; and every time
you use a spell-checker, a string-searching algorithm runs. Creating correct and efficient
algorithms is an art and a science, which takes both practice and creativity. Whether you
need to calculate the average of five numbers, sort a list of two million names, or guide a
rocket, an algorithm lurks in the background; the solution to your problem is an algorithm.
The study of algorithms is a cornerstone of computer science.
A programming language is your tool, a tool that you can use to investigate and imple-ment algorithms. With a programming language, such as Java, you can turn algorithms into
programs so that a computer finds the average, sorts the list, or guides the rocket. Programs
implement algorithms; programming makes algorithms come to life. As you work through
the problems and exercises in this real world example, you will increased your problem-solving skills, design
and implement your own algorithms, and, along the way, discover that programming with
Java is fun.