sads

Monday, 23 January 2017

Java Array



Java Array
ormally, array is a collection of similar type of elements that have contiguous memory location.
Java array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed set of elements in a java array.
Array in java is index based, first element of the array is stored at 0 index.

 

Advantage of Java Array

  • Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.
  • Random access: We can get any data located at any index position.

Disadvantage of Java Array

  • Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java.

Types of Array in java

There are two types of array.
  • Single Dimensional Array
  • Multidimensional Array

Example of single dimensional java array

Let's see the simple example of java array, where we are going to declare, instantiate, initialize and traverse an array.


class Testarray
{
public static void main(String args[])
{
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}
}

2. another example:

class Testarray1
{
public static void main(String args[])
{

int a[]={33,3,4,5};//declaration, instantiation and initialization

//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);

}
}

Passing Array to method in java

class TestarrayPassing
{
static void min(int arr[])
{
int min=arr[0];
for(int i=1;i<arr.length;i++)
 if(min>arr[i])
  min=arr[i];

System.out.println(min);
}

public static void main(String args[]){

int a[]={33,1,4,5};
min(a);//passing array to method

}
}

Multidimensional array in java

Example of Multidimensional java array

Let's see the simple example to declare, instantiate, initialize and print the 2Dimensional array.
class TestarrayMultiDimension
{
public static void main(String args[])
{

//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};

//printing 2D array
for(int i=0;i<3;i++)
{
 for(int j=0;j<3;j++)
{
   System.out.print(arr[i][j]+" ");
 }
 System.out.println();
}

}
}

What is the class name of java array?

In java, array is an object. For array object, an proxy class is created whose name can be obtained by getClass().getName() method on the object.

class TestarrayClassName
{
public static void main(String args[])
{
int arr[]={4,4,5};
Class c=arr.getClass();
String name=c.getName();
System.out.println(name);
}
}

Copying a java array

Syntax of arraycopy method

public static void arraycopy(Object src, int srcPos,Object dest, int destPos, int length)

Example of arraycopy method

class TestArrayCopy {
    public static void main(String[] args)
{
        char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' };
        char[] copyTo = new char[7];

        System.arraycopy(copyFrom, 2, copyTo, 0, 7);
        System.out.println(new String(copyTo));
    }
}


Addition of 2 matrices in java

class TestarrayAdd
{
public static void main(String args[])
{
//creating two matrices
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};

//creating another matrix to store the sum of two matrices
int c[][]=new int[2][3];

//adding and printing addition of 2 matrices
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}
}

Monday, 9 January 2017

Practise Best Samples

    class Student1{ 
     int id;//data member (also instance variable) 
     String name;//data member(also instance variable) 
     
     public static void main(String args[]){ 
      Student1 s1=new Student1();//creating an object of Student 
      System.out.println(s1.id); 
      System.out.println(s1.name); 
     } 
    } 


-------------------------------------
    class Student2{ 
     int rollno; 
     String name; 
     
     void insertRecord(int r, String n){  //method 
      rollno=r; 
      name=n; 
     } 
     
     void displayInformation(){System.out.println(rollno+" "+name);}//method 
     
     public static void main(String args[]){ 
      Student2 s1=new Student2(); 
      Student2 s2=new Student2(); 
     
      s1.insertRecord(111,"Karan"); 
      s2.insertRecord(222,"Aryan"); 
     
      s1.displayInformation(); 
      s2.displayInformation(); 
     
     } 
    } 

------------------------------------------

    class Rectangle{ 
     int length; 
     int width; 
     
     void insert(int l,int w){ 
      length=l; 
      width=w; 
     } 
     
     void calculateArea(){System.out.println(length*width);} 
     
     public static void main(String args[]){ 
      Rectangle r1=new Rectangle(); 
      Rectangle r2=new Rectangle(); 
     
      r1.insert(11,5); 
      r2.insert(3,15); 
     
      r1.calculateArea(); 
      r2.calculateArea(); 
    } 
    } 


--------------------------------------


    class Calculation{ 
     
     void fact(int  n){ 
      int fact=1; 
      for(int i=1;i<=n;i++){ 
       fact=fact*i; 
      } 
     System.out.println("factorial is "+fact); 
    } 
     
    public static void main(String args[]){ 
     new Calculation().fact(5);//calling method with annonymous object 
    } 
    } 

---------------------------------

    class Rectangle{ 
     int length; 
     int width; 
     
     void insert(int l,int w){ 
      length=l; 
      width=w; 
     } 
     
     void calculateArea(){System.out.println(length*width);} 
     
     public static void main(String args[]){ 
      Rectangle r1=new Rectangle(),r2=new Rectangle();//creating two objects 
       
      r1.insert(11,5); 
      r2.insert(3,15); 
     
      r1.calculateArea(); 
      r2.calculateArea(); 
    } 
    } 


-------------------------------
    class Calculation{ 
      void sum(int a,int b){System.out.println(a+b);} 
      void sum(int a,int b,int c){System.out.println(a+b+c);} 
     
      public static void main(String args[]){ 
      Calculation obj=new Calculation(); 
      obj.sum(10,10,10); 
      obj.sum(20,20); 
     
      } 
    } 

--------------------------------

    class Calculation2{ 
      void sum(int a,int b){System.out.println(a+b);} 
      void sum(double a,double b){System.out.println(a+b);} 
     
      public static void main(String args[]){ 
      Calculation2 obj=new Calculation2(); 
      obj.sum(10.5,10.5); 
      obj.sum(20,20); 
     
      } 
    } 

----------------------------
Que) Why Method Overloaing is not possible by changing the return type of method?

    class Calculation3{ 
      int sum(int a,int b){System.out.println(a+b);} 
      double sum(int a,int b){System.out.println(a+b);} 
     
      public static void main(String args[]){ 
      Calculation3 obj=new Calculation3(); 
      int result=obj.sum(20,20); //Compile Time Error 
     
      } 
    } 

--------------------------------

Can we overload main() method?


    class Overloading1{ 
      public static void main(int a){ 
      System.out.println(a); 
      } 
       
      public static void main(String args[]){ 
      System.out.println("main() method invoked"); 
      main(10); 
      } 
    } 

-----------------------
class OverloadingCalculation1{ 
  void sum(int a,long b){System.out.println(a+b);} 
  void sum(int a,int b,int c){System.out.println(a+b+c);} 
 
  public static void main(String args[]){ 
  OverloadingCalculation1 obj=new OverloadingCalculation1(); 
  obj.sum(20,20);//now second int literal will be promoted to long 
  obj.sum(20,20,20); 
 
  } 


---------------

    class OverloadingCalculation2{ 
      void sum(int a,int b){System.out.println("int arg method invoked");} 
      void sum(long a,long b){System.out.println("long arg method invoked");} 
     
      public static void main(String args[]){ 
      OverloadingCalculation2 obj=new OverloadingCalculation2(); 
      obj.sum(20,20);//now int arg sum() method gets invoked 
      } 
    } 

-----------------------------------------
Example of Method Overloading with Type Promotion in case of ambiguity
    class OverloadingCalculation3{ 
      void sum(int a,long b){System.out.println("a method invoked");} 
      void sum(long a,int b){System.out.println("b method invoked");} 
     
      public static void main(String args[]){ 
      OverloadingCalculation3 obj=new OverloadingCalculation3(); 
      obj.sum(20,20);//now ambiguity 
      } 
    } 

-----------------
Understanding the problem without method overriding

    class Vehicle{ 
      void run(){System.out.println("Vehicle is running");} 
    } 
    class Bike extends Vehicle{ 
       
      public static void main(String args[]){ 
      Bike obj = new Bike(); 
      obj.run(); 
      } 
    } 

--------------------------

Example of method overriding
    class Vehicle{ 
    void run(){System.out.println("Vehicle is running");} 
    } 
    class Bike2 extends Vehicle{ 
    void run(){System.out.println("Bike is running safely");} 
     
    public static void main(String args[]){ 
    Bike2 obj = new Bike2(); 
    obj.run(); 
    } 

-----------------------------





    class Bank{ 
    int getRateOfInterest(){return 0;} 
    } 
     
    class SBI extends Bank{ 
    int getRateOfInterest(){return 8;} 
    } 
     
    class ICICI extends Bank{ 
    int getRateOfInterest(){return 7;} 
    } 
    class AXIS extends Bank{ 
    int getRateOfInterest(){return 9;} 
    } 
     
    class Test2{ 
    public static void main(String args[]){ 
    SBI s=new SBI(); 
    ICICI i=new ICICI(); 
    AXIS a=new AXIS(); 
    System.out.println("SBI Rate of Interest: "+s.getRateOfInterest()); 
    System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest()); 
    System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest()); 
    } 
    } 

---------------------------------

Difference between method overloading and method overriding in java

There are many differences between method overloading and method overriding in java. A list of differences between method overloading and method overriding are given below:
No.Method Overloading Method Overriding
1)Method overloading is used to increase the readability of the program.Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
2)Method overloading is performed within class.Method overriding occurs in two classes that have IS-A (inheritance) relationship.
3)In case of method overloading, parameter must be different.In case of method overriding, parameter must be same.
4)Method overloading is the example of compile time polymorphism.Method overriding is the example of run time polymorphism.
5)In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.Return type must be same or covariant in method overriding.



Monday, 2 January 2017

Program for Prime Number

import java.util.*;
public class Test {
public static void main(String[] args) {
System.out.println("Welcome to Aavanthi Engineering College");
System.out.println("--------Prime Number--------");
Scanner sc = new Scanner(System.in);
System.out.println("Enter valid Number");
int n = sc.nextInt();
int c = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
if (c == 2) {
System.out.println(n + "is Prime Number");
} else {
System.out.println(n + "is not Prime Number");
}
}
}