• Click below to download Unit 2 programs in pdf file: -




Unit 2


/* 1.Write a program to sort the elements of one dimensional array. Read value of

array elements through command line argument. */


public class p1


{

public static void main(String[] args)


{


int i, j, n, temp;


n=args.length; 

int a[] = new int[n]; 


for(i=0; i<n; i++)

{

   a[i]=Integer.parseInt(args[i]);

}


for (i=0; i<n; i++) 

{

for (j=i+1; j<n; j++)

{

if (a[i]>a[j])


{

temp = a[i];

a[i] = a[j]; 

a[j] = temp;

}

}

}

System.out.println("Array Elements after sorting:"); 


for (i=0; i<n; i++) 

{

System.out.println(a[i]);

}

}

}



/* 2.Write a program to create an array to store 5 integer values. Also initialize the

array with 5 numbers and display the array Elements in reverse order. */


import java.util.*;


public class p2

{

public static void main(String []args)

{

int i;

   Scanner sc= new Scanner(System.in);


int arr[]= new int[5];


System.out.println("enter the elements of array :");

for (i=0; i<5; i++)

{

arr[i]=sc.nextInt();

}

System.out.println("elements of array in reverse order :");


for (i=4; i>=0; i--)

{

System.out.println(arr[i]);

}

}

}



/* 3.Write a program to find sum of two matrices of 3 x3. */


import java.util.*;


public class p3

{

   public static void main(String []args)

{

      int i,j;


Scanner sc=new Scanner(System.in);


     int arr[][] = new int[3][3];

     System.out.println("Enter the elements of first matrix :");

    for(i=0; i<arr.length; i++)

{

      for (j=0; j<arr[i].length; j++)

{

     arr[i][j]=sc.nextInt();

}

}


    int arr1[][] = new int[3][3];

    System.out.println("Enter the elements of second matrix :");

    for(i=0; i<arr1.length; i++)

{

      for (j=0; j<arr1[i].length; j++)

{

     arr1[i][j]=sc.nextInt();

}

}


 int arr2[][] = new int[3][3];


    for(i=0; i<arr2.length; i++)

{

      for (j=0; j<arr2[i].length; j++)

{


     arr2[i][j]= arr[i][j]+ arr1[i][j];

}

   

}

System.out.println("----Addtion perfomed----");


    for(i=0; i<arr2.length; i++)

{

      for (j=0; j<arr2[i].length; j++)

{

    System.out.println(  arr2[i][j]+"\t");

     }

   }

  }

}



/* 4.Write program to create an array of company name and another array of price

quoted by the company. Fetch the company name who has quoted the lowest

amount. */


class p4

{

public static void main(String ar[])

{

    int n,i;


    String name[]={"Realme","Redmi","Vivo","Oppo","Apple"};

    int price[]={12000,9000,11000,19000,110000};


    n=price.length;


    int small=price[0];


    for(i=1;i<n;i++)

    {

        if(small>price[i])

        {

            small=price[i];

        }

    }

    for(i=0;i<n;i++)

    {

        if(small==price[i])

        {

            System.out.println(name[i]);

        }

    }

  }

}



/* 5.Write an interface called numbers, with a method in Process(int x, int y). Write a

class called Sum, in which the method Process finds the sum of two numbers

and returns an int value. Write another class called Average, in which the

Process method finds the average of the two numbers and returns an int. */


import java.util.*;


interface numbers

{

  public int process( int x, int y);

}

 

class sum implements numbers

{

  int n;

  public int process(int x,int y)

  n=x+y;

  System.out.println("Sum :"+n);

  return n;

}

}


class avg implements numbers 

{

  int m;

public int  process( int x, int y)

{  

  m=((x+y)/2);

  System.out.println("Avg :"+m);

  return m;

}

}


class p5

{

   public static void main(String []args)

 { 

     Scanner sc = new Scanner(System.in);

int x,y;


    System.out.println("enter the numbers :");

    x=sc.nextInt();

    y=sc.nextInt();


 sum s=new sum();

 s.process(x,y);


 avg a=new avg();

 a.process(x,y);

}

}



/* 6.Create a class called NumberData that accept any array of the five numbers.

Create a sub class called Numplay which provides methods for followings:

1. Display numbers entered.

2. Sum of the number.

3. Average of the numbers.

4. Maximum of the numbers.

5. Minimum of the numbers.

Create a class that provides menu for above methods. Give choice from the

command-line argument. */


import java.util.*;

class NumberData2

{

    int ch;

    int sum=0;

    int avg=0;

    int max,min;

}

class p6 extends NumberData2

{

    public void display()

    {

        Scanner sc=new Scanner(System.in);

        System.out.println("enter your choice:");

        System.out.println("choice 1:Display numbers entered");

        System.out.println("choice 2:Sum of the number");

        System.out.println("choice 3:Average of the number");

        System.out.println("choice 4:Maximum of the numbers");

        System.out.println("choice 5:Minimum of the numbers");

        ch=sc.nextInt();

        System.out.println("Enter Values :");

        int array[]=new int[5];

 

        for (int i=0;i<5;i++) 

        {

            array[i]=sc.nextInt(); 

        }

    switch(ch)

    {

    case 1:

        for (int i=0;i<5;i++)

        {

            System.out.println("Values are :"+array[i]);

        }

        break;

    case 2:

        for (int i=0;i<5;i++)

        {

            sum = sum + array[i];

        }

        System.out.println("Sum:"+sum);

        break;

    case 3:

        for (int i=0;i<5;i++)

        {

            sum = sum + array[i];

        }

        avg=sum/5;

        System.out.println("Sum:"+sum);

        System.out.println("Avg:"+avg);

        break;

    case 4:

        max=array[0];

        for(int i=0;i<5;i++)

        {

            if(max<array[i])

            {

                max=array[i];

            }

        }

        System.out.println("Maximum value:"+max);

        break;

    case 5:

        min=array[0];

        for(int i=0;i<5;i++)

        {

            if(min>array[i])

            {

                min=array[i];

            }

        }

        System.out.println("Maximum value:"+min);

        break;

    default:

        break;

    }

}

    public static void main(String[] args)

    {

    Numplay n=new Numplay();

    n.display();

    }

}



/* 7.Declare an abstract class Vehicle with an abstract method named numWheels(

).provide subclasses Car and Truck that each implements this method. Create

instance of these subclasses and demonstrate the use of this method */


abstract class vehicle

{

 abstract void numwheels();

}


class car extends vehicle 

{


void numwheels()

{

System.out.println("car has four wheels");

}

}


class truck extends vehicle 

{

 

void numwheels()

{

System.out.println("truck has six wheels");

}

}


class p7

{

public static void main(String []args)

{

   car c=new car();

    c.numwheels();


truck t=new truck();

t.numwheels();

}

}



/* 8.Write an interface called Exam with a method Pass(int mark) that returns a

Boolean. Write another interface called Classify with a method Division(int

average) which returns a string. Write a class called Result which implements

both Exam and Classify. The pass method should return true if the marks is

greater than or equal to 35 else false. The division method must return “First”

when the parameter average is 60 or more, “second” when average is 50 or

more but below 60, “no division” when average is less than 50. */


interface exam

{

    public boolean Pass(int mark);

}

interface classify

{

    public String Division(int average);

}

class result implements exam,classify

{

    public boolean Pass(int mark)

    {

        if(mark>=35)

        {

            System.out.println("Pass");

        }

        else

        {

            System.out.println("Fail");

        }

    return true;

    }

    public String Division(int average)

    {

    if(average>=60)

    {

        System.out.println("First");

    }

    else if(average>=50 || average<60)

    {

        System.out.println("Second");

    }

    else

    {

        System.out.println("No division");

    }

    return "pass";

    }

}

class p8

{

    public static void main(String[] args)

    {

        result r=new result();

        r.Pass(67);

        r.Division(19);

    }

}



/* 9.Create class calculation with an abstract method area( ). Create Rectangle and

Triangle subclasses of calculation and find area of rectangle and triangle. */


abstract class calculation

{

    double height,width;

    calculation(double height,double width)

    {

        this.height=height;

        this.width=width;

    }

    abstract double area();

}

class rectangle extends calculation

{

    rectangle(double h,double w)

    {

        super(h,w);

    }

    double area()

    {

        return height*width;

    }

}

class triangle extends calculation

{

    triangle(double h,double w)

    {

        super(h,w);

    }

    double area()

    {

        return 0.5*height*width;

    }

}

class p9

{

    public static void main(String ar[])

    {

        calculation cl;

        rectangle r=new rectangle(10,15);

        cl=r;

        System.out.println("area of rectangle is "+cl.area());

        triangle t=new triangle(18,20);

        cl=t;

        System.out.println("area of triangle is "+cl.area());

    }

}



/* 10.The abstract Vegetable class has four subclasses named cabbage, carrot and

potato. Write an application that demonstrates how to establish this class

hierarchy. Declare one instance variable of type string that indicates the color of

a vegetable. Create and display instances of these object. Override the toString()

method of object to return a string with the name of the vegetable and its color. */


abstract class Vegetable

{

    String color;

    String name;

    public abstract String toString();

}


class Cabbage extends Vegetable

{

    public String toString()

    {

        return "Name : " + name + "\nColor: " + color;

    }

    Cabbage(String str,String clr)

    {

        name = str;

        color = clr;

    }

}


class Carrot extends Vegetable

{

    public String toString()

    {

        return "Name : " + name + "\nColor: " + color;

    }

    

    Carrot(String str,String clr)

    {

        name = str;

        color = clr;    

    }

}


class Potato extends Vegetable

{

    public String toString()

    {

        return "Name : " + name + "\nColor: " + color;

    }

    

    Potato(String str,String clr)

    {

        name = str;

        color = clr;    

    }

}


class p10

{

    public static void main(String[] ar)

    {

        Cabbage C = new Cabbage("Cabbage","Green");

        Carrot  CR = new Carrot("Carrot","Orange");

        Potato P = new Potato("Potato","Yellow");

        

        System.out.println(C.toString());

        System.out.println(CR.toString());

        System.out.println(P.toString());

    }

}