0% found this document useful (0 votes)
15 views6 pages

Asg202501072140259944 0 17

The document describes a Java program that includes a class InsSort for sorting an array of integers in descending order and calculating the average of odd numbers. It also defines interfaces Animal, Printable, and Scannable, along with their implementations in Dog, Cat, and PrinterScanner classes. Additionally, it outlines a structure for calculating the volume of a cylinder using inheritance and interfaces, specifically detailing the CalVol class that extends Base and implements Data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Asg202501072140259944 0 17

The document describes a Java program that includes a class InsSort for sorting an array of integers in descending order and calculating the average of odd numbers. It also defines interfaces Animal, Printable, and Scannable, along with their implementations in Dog, Cat, and PrinterScanner classes. Additionally, it outlines a structure for calculating the volume of a cylinder using inheritance and interfaces, specifically detailing the CalVol class that extends Base and implements Data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*

A class InsSort contains an array of integers which sorts the elements in a particular order.
Some of the members of the class are given below.
Class name : InsSort
Data members/instance variables:
arr[ ] : stores the array elements
size : stores the number of elements in the array
Methods / Member functions:
InsSort(int s) : constructor to initialise size = s
void getArray( ) : accepts the array elements
void insertionSort( ) : sorts the elements of the array in descending order using the Insertion Sort
technique
double find( ) : calculates and returns the average of all the odd numbers in the array
void display( ) : displays the elements of the array in a sorted order along with the average of all the odd
numbers in the array by invoking the function find( ) with an appropriate message
Specify the class InsSort giving details of the constructor( ), void getArray( ), void insertionSort( ),
double find( ) and void display( ). Define a main( ) function to create an object and call all the functions
accordingly to enable the task.
*/
import java.util.*;
class InsSort
{
int arr[];
int size;
static Scanner sc = new Scanner(System.in);
public InsSort(int s)
{
size = s;
arr=new int[size];
}
public void getArray()
{
System.out.println("Enter the array elements:");
for(int i=0;i<size;i++)
arr[i] = sc.nextInt();
}
public void insertionSort()
{
int j,current;
for(int i=1;i<size;i++)
{
current = arr[i];
j=i-1;
while (j>=0 && arr[j]<current)
{
arr[j+1]= arr[j] ;
j=j-1 ;
}
arr[j+1]=current ;
}
}

Page 1 of 6
public double find()
{
int sum=0,count=0;
for(int i=0;i<size;i++)
{
if(arr[i]%2==1)
{
sum=sum+ arr[i];
count++;
}
}
double avg=(double)sum/count;
return avg;
}
public void display()
{
System.out.println("Sorted array:");
for(int i=0;i<size;i++)
System.out.println(arr[i]);
System.out.println("Average of all odd numbers in the array ="+find());
}
public static void main()
{
System.out.println("Enter the number of elements in the array:");
int s=sc.nextInt();
InsSort obj = new InsSort(s);
obj.getArray();
obj.insertionSort();
obj.display();
}
}
/*
OUTPUT
Enter the number of elements in the array:
10
Enter the array elements:
1
7
8
9
2
3
4
5
6
10
Sorted array:
10
9
8
7
6
5
4
3
2
1
Average of all odd numbers in the array =5.0
*/

Page 2 of 6
// Defining an interface
interface Animal
{
void eat(); // abstract method
void sleep(); // abstract method
}
// Implementing the interface in the Dog class
class Dog implements Animal
{
// Providing implementation for eat method
public void eat()
{
System.out.println("Dog is eating.");
}
// Providing implementation for sleep method
public void sleep()
{
System.out.println("Dog is sleeping.");
}
}
// Implementing the interface in the Cat class
class Cat implements Animal
{
public void eat()
{
System.out.println("Cat is eating.");
}

public void sleep()


{
System.out.println("Cat is sleeping.");
}
}

// Main class
public class Main_Interface1
{
public static void main(String[] args)
{
Animal dog = new Dog(); // Polymorphic behavior
Animal cat = new Cat();

dog.eat();
dog.sleep();

cat.eat();
cat.sleep();
}
}

Page 3 of 6
// Interface 1
interface Printable
{
void print();
}

// Interface 2
interface Scannable
{
void scan();
}

// Class implementing both interfaces


class PrinterScanner implements Printable, Scannable
{
public void print()
{
System.out.println("Printing document...");
}

public void scan()


{
System.out.println("Scanning document...");
}
}

// Main class
public class Main_Interface2
{
public static void main(String[] args)
{
PrinterScanner device = new PrinterScanner();
device.print();
device.scan();
}
}

Page 4 of 6
/*
An interface Data is defined with a data member and a method volume( ) which returns
the volume of the implementing shape. A super class Base has been defined to contain
the radius of a geometrical shape. Define a sub class CalVol which uses the properties
of the interface Data and the class Base and calculates the volume of a cylinder.
The details of the members of the interface and both the classes are given below:

Interface name : Data


Data member :
double pi : initialize pi = 3.142
Member functions/methods:
double volume( ) :

Class name : Base


Data member/instance variable:
rad : to store the radius in decimal
Member functions/methods:
Base(…) : parameterized constructor to initialize the data member
void show( ) : displays the radius with an appropriate message

Class name : CalVol


Data member/instance variable:
ht : to store the height in decimal
Member functions/methods:
CalVol(…) : parameterized constructor to initialize the data members of
both the classes
double volume( ) : calculates the volume of a sphere by using the formula
( pi x radius^2 x height )
void show( ) : displays the data members of both the classes and the volume
of the sphere with appropriate message

Assume that the interface Data and the super class Base has been defined.
Using the concept of inheritance, specify the class CalVol giving the details of the
constructor(…), double volume( ) and void show( ).
The interface, super class, main function, and algorithm need NOT be written.
*/
import java.util.*;

class ICS2020
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);

double r,h;
System.out.print("Enter Radius & Height : ");
r = Double.parseDouble(in.nextLine());
h = Double.parseDouble(in.nextLine());

CalVol obj = new CalVol(r,h);


obj.show();
}
}

Page 5 of 6
interface Data
{
double pi=3.142d;
double volume();
}

class Base
{
double rad;

Base(double r)
{
rad = r;
}

void show()
{
System.out.println("Radius = " + rad);
}
}

class CalVol extends Base implements Data


{
double ht;

CalVol(double r, double h)
{
super(r);
ht=h;
}

public double volume()


{
double x = pi * rad * rad * ht;
return x;
}

void show()
{
super.show();
System.out.println("Height= " + ht);
System.out.println("Volume= " + volume());
}
}

Note : For Exams purpose write ONLY this Sub class : class CalVol extends Base implements Data

Page 6 of 6

You might also like