Java Programming Practical 9
Question 1
Develop a program to find area of rectangle and circle using interfaces.
InterfaceDemo.java
Java
interface Shape
{
void calculateArea();
}
class Rectangle implements Shape
{
float length,width;
Rectangle(float l, float w)
{
length = l;
width = w;
}
public void calculateArea()
{
float a=length * width;
System.out.println("Area of Rectangle: " + a);
}
}
class Circle implements Shape
{
float radius;
Circle(float r)
{
radius = r;
}
public void calculateArea()
{
float a= 3.14f * radius * radius;
System.out.println("Area of Circle: " + a);
}
}
public class InterfaceDemo
{
public static void main(String[] args)
{
Rectangle rectangle = new Rectangle(10.0f, 5.0f);
Circle circle = new Circle(7.0f);
rectangle.calculateArea();
circle.calculateArea();
}
}
Output
