Array, Inheritance and Interface Program


1 Write a program to sort the elements of one dimensional array. Read value of array elements through command line argument.

import java.util.*;

public class Array1

{

public static void main(String[] args)

{

int i,j,count,temp;

Scanner s1=new Scanner(System.in);

System.out.println("How many numbers you want to enter:- ");

count=s1.nextInt();

int no[]=new int[count];

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

{

System.out.println("Enter "+(i+1)+"Element:- ");

no[i]=s1.nextInt();

}

for(i=0;i<count-1;i++)

{

for(j=0;j<count-i-1;j++)

{

if(no[j]>no[j+1])

{

temp=no[j];

no[j]=no[j+1];

no[j+1]=temp;

}

}

}

System.out.println("After Accending Array is:-");

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

{

System.out.println(no[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 Array2

{

public static void main(String[] args)

{

int i;

int no[]=new int[5];

Scanner s1=new Scanner(System.in);

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

{

System.out.println("Enter " +(i+1)+"Element:- ");

no[i]=s1.nextInt();

}

System.out.println("Array in Reversed Order is:- ");

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

{

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

}

}

}

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

public class addMatrix

{

public static void main(String[] args)

{

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

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

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

int e[][]=new int[3][3];

int i,j;

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

{

for(j=0;j<3;j++)

{

e[i][j]=a[i][j]+b[i][j]+c[i][j];

System.out.println(e[i][j]+" ");

}

System.out.println();

}

}

}

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.

import java.util.*;

public class Array4

{

public static void main(String[] args)

{

String name[]=new String[5];

int price[]=new int[5];

Scanner s1=new Scanner(System.in);

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

{

System.out.println("Enter "+(i+1)+"  Company Name:- ");

name[i]=s1.nextLine();

s1.nextLine();

System.out.println("Enter Price Quoted by company:- ");

price[i]=s1.nextInt();

}

int min=price[0];

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

{

if(min>price[i])

{

min=price[i];

}

}

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

{

if(min==price[i])

{

System.out.println("The Company quoted Lowest Amount is:- "+price[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

{

abstract int process(int x,int y);

}

class Sum implements Numbers

{

public int process(int x,int y)

{

int ans=x+y;

return(ans);

}

}

class Average implements Numbers

{

public int process(int x,int y)

{

int ans=(x+y)/2;

return(ans);

}

}

public class interface1

{

public static void main(String[] args)

{

Scanner s1=new Scanner(System.in);

int a,b;

System.out.println("Enter the value of a:- ");

a=s1.nextInt();

System.out.println("Enter the value of b:- ");

b=s1.nextInt();

Sum s=new Sum();

System.out.println("Addition is:-  "+s.process(a,b));

Average a1=new Average();

System.out.println("Avreage is:- "+a1.process(a,b));

}

}

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 NumberData

{

int no[]=new int[5];

Scanner s1=new Scanner(System.in);

void createarray()

{

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

{

System.out.println("Enter "+(i+1)+"Number");

no[i]=s1.nextInt();

}

}

}

class Numplay extends NumberData

{

void display()

{

createarray();

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

{

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

}

}

void sum()

{

createarray();

int sum=0;

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

{

sum=sum+no[i];

}

System.out.println(sum);

}

void avg()

{

createarray();

int sum=0;

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

{

sum=sum+no[i];

}

float a= sum/5;

System.out.println(a);

}

void max()

{

createarray();

int max=no[0];

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

{

if(no[i]>max)

{

max=no[i];

}

}

System.out.println(max);

}

void min()

{

createarray();

int min=no[0];

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

{

if(min>no[i])

{

min=no[i];

}

}

System.out.println(min);

}

}

public class Array6

{

public static void main(String[] args)

{

Scanner s2=new Scanner(System.in);

Numplay n1=new Numplay();

int ch;

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

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

System.out.println(" 3. Average of the numbers.");

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

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

System.out.println("Enter Your choice:- ");

ch=s2.nextInt();

switch(ch)

{

case 1:

n1.display();

break;

case 2:

n1.sum();

break;

case 3:

n1.avg();

break;

case 4:

n1.max();

break;

case 5:

n1.min();

break;

}

}

}

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 Class Executed");

}

}

class truck extends Vehicle

{

void numWheels()

{

System.out.println("Truck Class Execuated");

}

}

public class abstract1

{

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 Resualt 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("Secound");

}

else

{

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

}

return "Pass";

}

}

public class student

{

public static void main(String[] args)

{

Resualt r1=new Resualt();

r1.Pass(55);

r1.Division(23);

}

}

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

{

int l,b;

abstract void area();

}

//public

class Rectangle extends Calculation

{

Rectangle(int p,int q)

{

l=p;

b=q;

}

public void area()

{

System.out.println("\nArea of Rectangle is : " +(l*b));

}

}

//public

class Triangle extends Calculation

{

Triangle(int p,int q)

{

l=p;

b=q;

}

public void area()

{

System.out.println("\nArea of Rectangle is : " +((l*b)/2));

}

}

//public

class u2_p9

{

public static void main(String[] args)

{

Rectangle r1=new Rectangle(5,7);

r1.area();

Triangle t1=new Triangle(7,8);

t1.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 vegatable

{

String name;

String color;

public abstract String toString();

}

class Carrot extends vegatable

{

public String toString()

{

return "Name:- "+name+"Color:-"+color;

}

Carrot(String nam, String col)

{

name=nam;

color=col;

}

}

class Cabbage extends vegatable

{

public String toString()

{

return "Name:- "+name+"Color:-"+color;

}

Cabbage(String nam, String col)

{

name=nam;

color=col;

}

}

class potato extends vegatable

{

public String toString()

{

return "Name:- "+name+"Color:-"+color;

}

potato(String nam, String col)

{

name=nam;

color=col;

}

}

class u2_p10

{

public static void main(String[] args)

{

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

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

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

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

potato p=new potato("Potato","Brawn");

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

}

}

The End

No comments:

Post a Comment

Aarsh Prajapati

Welcome! Java Programs And Theory  Practical    Theory Java Basic Program Java Simple Program Array, Inheritance and Interface Program Packa...