Friday 29 December 2017

4.1 Object Oriented Programming (OOPS Concept)

Class and Object


Any entity that has state and behavior is known as an object. For example: table, book , building, pen etc. It can be physical and logical.

The class defines the structure of an object. It is a blueprint.

In java this blueprint is called a class.

And every object have some behaviour and attributes.

Attributes are data members such as variables.

Behaviour is the member functions or you can say methods of the class.



Read More »

3.3 Different ways of declaring main method

public static void main(String[] args)

static public void main(String[] args)

static public void main(String []args)

public static void main(String args[])

public static void main(String... args)

public static synchronized void main(String... args)

public static strictfp void main(String... args)

public static final void main(String... args)



Important points about main method.



Read More »

3.2 Var args [Variable Arguments]

Variable Arguments - These are used when you don't know the exact number of arguments to be used in your method.

Read More »

3.1 Array

Array - An array is the collection of similar kind of element stored in a single variable.

example int A[3] = {1,2,3}

Array index start with 0.

If you try to store/print value outside the index size, you'll get ArrayIndexOutOfBoundsException.

1. Single dimensional array

int a[] = {1 , 2, 3, 4, 5, 6};
           1 2 3 4 5 6

2. 2 D Array
          int a[][] = {
                             {1, 2, 3, 4, 5, 6}
                             {7, 8, 9, 1, 2, 3}
                           };

           1 2 3 4 5 6
           7 8 9 1 2 3

3. 3D Array
          int b[][][] = new int[2][2][2]

4. Jagged Array

          int a[][] = {
                             {1, 2, 3, 4}
                             {7, 8, 9}
                             {2, 4, 5, 6, 7, 8}
                           };


          int k[][] = new int [3][];
          k[0]= new int[4];
          k[1]= new int[3];
          k[2]= new int[5];


See the below example to use an array



Read More »

Thursday 28 December 2017

2.6 Basic Program for Practice

1. Palindrome


class palindrome
{
public static void main(String args[])
{
int r,sum=0,temp;    
int n=12321;//It is the number variable to be checked for palindrome  
  
temp=n;    
while(n>0)
{    
r=n%10;  //getting remainder  
sum=(sum*10)+r;    
n=n/10;    
}    
  if(temp==sum)    
   System.out.println("palindrome number ");    
  else    
   System.out.println("not palindrome");    
}

}

2. Prime Number


public class PrimeNumber{    
public static void main(String args[])
{    
  int i,flag=0;      
  int n=3;//check this number    
  int m=n/2;      
  if(n==0||n==1){  
   System.out.println(n+" is not prime number");      
  }
  else
  {
for(i=2;i<=m;i++)
{      
if(n%i==0)
{      
System.out.println(n+" is not prime number");      
flag=1;      
break;      
}      
}      
if(flag==0)  { System.out.println(n+" is prime number"); }  
}  
}    
}   


3. Factorial


class Factorial
{  
public static void main(String args[])
{  
  int i,fact=1;  
  int number=5;//Check this number    
  for(i=1;i<=number;i++){    
  fact=fact*i;    
}    
  System.out.println("Factorial of "+number+" is: "+fact);    
}  
}  

4. Armstrong Number

class Armstrong
{  
  public static void main(String[] args)  
  {  
int c=0,a,temp;  
int n=153;//Check this number  
temp=n;  
while(n>0)  
{  
a=n%10;  
n=n/10;  
c=c+(a*a*a);  
}  
if(temp==c)  
System.out.println("armstrong number");   
else  
System.out.println("Not armstrong number");   
}  
}  

5. Fibonacci Series

class Fibonacci
{
public static void main(String args[])
{
int n=20;
int j=0;
int k=1;
int num=0;
System.out.println(k);
while (num<=n)
{
num=j+k;
System.out.println(num);
j=k;
k=num;
}
}
}
Read More »

2.5 Patterns printing

1. Logic to print different patterns is
first loop will decide the number of rows
Second loop will decide the number of columns
you have to manipulate the second loop logic to get the desired pattern.



public class Pattern {

        public static void main(String[] args) 
{
               //Pattern1
                for(int i=1; i<= 5 ;i++)
{
                        for(int j=0; j < i; j++)
{
                               System.out.print("*");
                            }
                        //generate a new line
                        System.out.println("");
}

//Pattern2
System.out.println("");
  for(int i=5; i>0 ;i--){
                       
                        for(int j=0; j < i; j++){
                                System.out.print("*");
                        }
                       
                        //generate a new line
                        System.out.println("");
                }

System.out.println("");
//Pattern3
for(int i=1; i<= 5 ;i++){
                       
                        for(int j=0; j < i; j++){
                                System.out.print(j+1);
                        }
                       
                        System.out.println("");
                }
        }
}

2. Ignore the syntax of below diagram - understand the logic only




3. Fibonacci Series

class Fibonacci
{
public static void main(String args[])
{
int n=20;
int j=0;
int k=1;
int num=0;
System.out.println(k);
while (num<=n)
{
num=j+k;
System.out.println(num);
j=k;
k=num;
}
}

}



Read More »

2.4 For, While, Do While loop and Break statement

Loops normally use to do a repeated task by avoiding the duplicate work.

All the looping control statements are easy and you can use any of them as per your understanding.

class Loop
{
public static void main(String args[])
{
//for loop
for(int i=1;i<=10;i++)
{
System.out.println(i);
}

//While loop
int j=1;
while(j<=10)
{
System.out.println(j);
j++;
}

//Do while
//This loop will run at least once and 
//then it will check for the condition.
int k=15;
do{
System.out.println(k);
}while(k<=10);

//labeled break statement
Bikesh:    // label
for(int p=1;p<=5;p++)
{
for(int q=1;q<=5;q++)
{
if(p==3)
break; // normal break
if(p==4)
break Bikesh; // labelled break
System.out.print("* ");
}
System.out.println();
}
}
}
Read More »

2.3 Switch Case

Switch case in java is used when you have to use multiple if else statements.


class SwitchCase
{
public static void main(String args[])
{
int n=4;
switch(n)
{
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 4:
System.out.println("Four");
break;
default:
System.out.println("Some number");
}

// if you not use break then all the out statement after the case match will print
// Switch case can be used with String , character as well.
}
}
Read More »

2.2 Trick in Java

1. Hello World without main method.

This was possible before 1.7. Logic behind this is , Static block executes before main method.
It will compile but it will not run in 1.7 and above.

to make it run you must have a main class
class Trick1 {
    static {
        System.out.println("Hello World");
        System.exit(0);
    }

    public static void main(String[] args) {}
}



2. Hello World without using semicolon.


Read More »

2.1 If Else and Ternary operator

Below is the example of even odd , Biggest number and ternary operator.

class IfElse
{
public static void main(String args[])
{
//If else
int a=5;

if(a%2==0)
{
System.out.println("Even");
}
else
{
System.out.println("Odd");
}

   //if else if
int p=4;
int q=9;
int r=2;

if (p>q && p>r)
{
System.out.println(p+" is greater than "+q+" and "+r);
}
else if (q>p && q>r )
{
System.out.println(q+" is greater than "+p+" and "+r);
}
else
{
System.out.println(r+" is greater than "+p+" and "+q);
}

//ternary operator
int x=4;
int y=5;
int z=x>y?2:10; // x is less than y therefore it will give 10
System.out.println(z);


//ternary operator is not the exact replacement of if else
Object obj1;
if(true)
{
obj1=new Integer(10);
System.out.println(obj1);
}
else
{
obj1= new Double(20.0);
System.out.println(obj1);
}
//the if else answer will be 10 but ternary output will be 10.0
// in ternary the datatype of output will be the biggest datatype from the both of the variable
Object obj2;
System.out.println(true?new Integer(10):new Double(20.0));
System.out.println(false?new Integer(10):new Double(20.0));
}
}
Read More »

1.5 Operators

1. Short Hand Operators += , -=, *=,  /=

int a=4;
a+=1; //a=a+1 short hand operator example
System.out.println(a);



2. Binary Literals 
To print binary to decimal we can use "0b" or "0B"
int n= 0b1001;
int n= 0B1001;
underscore is introduced in java 7

3. Pre-increment and Post-increment

int i=5;
i=i++;  Here i++ means
temp=i and increment
i=temp and increment
value of i will be 5

int i=5;
int j=i++;
here i++ means
temp=i and increment
j=temp and increment
value of j will be 5 and i will be 6. 


4. Bitwise AND &  , OR |

int a=25;
int b=15;

int c=a & b;
a=25 1 1 0 0 1
b=15 0 1 1 1 1
c=9   0 1 0 0 1

int d= a | b;
a=25 1 1 0 0 1
b=15 0 1 1 1 1
c=31 1 1 1 1 1

5. LeftShift << and RightShift >> operator

int a=8;
int b= a << 2;

b=32 it will add two more zeros at the end
a=8  1 0 0 0 left shift 1 0 0 0 0 0 which is the binary for 32

int a=25;
int b= b >> 2;

b=6 it will delete the last two digits
a=25 1 1 0 0 1 right shift 0 0  1 1 0 which is the binary for 6

6. Boolean (True or False)

boolean b=true;
System.out.println(b); // true
b=!b;
System.out.println(b); // false
Read More »

Wednesday 27 December 2017

1.4 Additional Knowledge

1. Class name can be different as the file name.

class A{
    public static void main(String args[])
    {
         System.out.println("Test");
    }
}

Save it as B.java
after compiling B.java  -----  javac B.java
you'll get a class file with name A.class. To run this code you have to write java A instead of java B.

2. Comments

/*
Multiple line comments
Multiple line comments
*/

// Single line comment


3. Concatenation operator = "+"

int a,b,c;
a=5;
b=5;
c=a+b;
System.out.println("Addition of "+a+ " and "+b+" is "+c);

4. printf in java
System.out.printf("Addition of %d and %d is 5d",a,b,c);


Read More »

1.3 Variables and Datatype

Variable : A variable is a named storage location where you can store different values.

Primitive variables : int, float, double, long, char , byte , boolean 

Datatype : Type of data store in a variable is called datatype



You can't store large value in small variable.


Different types of variable with their size and default values



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

int a=3;
int b=5;
System.out.println(a+b);

float p=3.4f;
float q=3.5f;
System.out.println(p+q);

char j='A';
System.out.println(j);
System.out.println((int)j);

int t=78;
System.out.println(t);
System.out.println((char)t);
}
}



Precision

Read More »

1.2 - First program in Java

Download JDK (Java Development Kit from oracle official website)


NOTE : Start working in Notepad . It will help you to memorize the syntax easily.

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


Read More »

1.1 History of Java

Java developed by Sun microsystem which lead by Mr. James Gosling.


The motive to develop java is to create something which is platform independent.

Initially it is developed for electronic appliances like set top boxes and called as "GREEN TALK".

later it is known as "Oak(1991)"

Java got patent in 1993 and later launched its first version in 1995.

The main feature of this programming language is Platform independent.

Code (.java) --> compiler will compile it into .class file which is a byte code. 

This byte code will run into JVM.

So it is must to have JVM to run any java code. By default windows, linux provides JVM.



Read More »