Java /Bio Java Journal Date of Performance: 14/03/2024
Practical 8
Programs on Interface
1. Write a program to demonstrate the use of the interface to calculate the addition,
subtraction, multiplication, and division of two numbers. Create a method of the
arithmetic operator.
Logic:
The main method initializes a reference variable c of type A and assigns it an instance of the
cali class.
The cali class implements the A interface, which declares a method air(double a, double b).
The air method of the cali class overrides the method declared in the A interface.
Within the air method, it performs addition, subtraction, division, and multiplication operations
on the parameters a and b, and prints the results.
When the main method is executed, it calls the air method through the interface reference c,
resulting in the execution of the overridden air method in the cali class.
The program demonstrates the concept of interface implementation in Java, where a class
implements the methods defined in an interface, providing its own implementation.
Code:
BID-21012 108
Java /Bio Java Journal Date of Performance: 14/03/2024
Output:
BID-21012 109
Java /Bio Java Journal Date of Performance: 14/03/2024
2. Write a program to demonstrate the use of Interface calculate the area of the following
figures:
i. Rhombus
ii. Triangle
iii. Rectangle
iv. Circle
Logic:
The Java program defines an interface Figure for calculating the area of geometric figures.
It includes classes Rhmbs, Tri, Rect, and Circle, each implementing the Figure interface and
providing specific implementations for calculating the area of a rhombus, triangle, rectangle, and
circle, respectively.
The Figure interface declares a method calculateArea() to calculate the area of a geometric figure.
The Rhmbs, Tri, Rect, and Circle classes implement the Figure interface and provide specific
implementations for calculating the area of a rhombus, triangle, rectangle, and circle, respectively.
Each class includes instance variables representing the dimensions of the respective geometric
figure and implements the calculateArea() method accordingly.
In the main method of the E8Q2 class:
Instances of each geometric figure are created using the Figure interface.
The calculateArea() method is called for each instance to calculate and display the area of the
respective figure.
The program demonstrates polymorphism in Java, where different classes implement the same
interface and provide different implementations for the interface method.
BID-21012 110
Java /Bio Java Journal Date of Performance: 14/03/2024
Code:
BID-21012 111
Java /Bio Java Journal Date of Performance: 14/03/2024
Output:
BID-21012 112
Java /Bio Java Journal Date of Performance: 14/03/2024
3. Write a program to demonstrate the use of Interface calculate the surface area and
volume of the following figures:
i. Sphere
ii. Cylinder
iii. Cone
iv. Cube
v. Prism
Logic:
The E8Q3 class serves as the entry point of the program, creating instances of shape classes and
invoking their sav() methods to calculate and print surface area and volume.
The program includes an interface Shap representing the common behavior of shape classes.
It defines several classes implementing the Shap interface to represent different geometric
shapes: shp, cy, co, cu, and pr.
Each shape class provides an implementation for the sav() method to calculate and print the
surface area and volume of the respective shape.
The E8Q3 class:
Creates instances of shape classes.
Invokes the sav() method of each shape to calculate and print its surface area and volume.
The program utilizes interface implementation to provide a common behavior across different
shape classes.
It performs calculations for surface area and volume of various shapes and displays the results.
BID-21012 113
Java /Bio Java Journal Date of Performance: 14/03/2024
Code:
BID-21012 114
Java /Bio Java Journal Date of Performance: 14/03/2024
Output:
BID-21012 115