Create an interface named Shape with the following method si
Solution
Shape.java
public interface Shape {
double area();
}
________________
Circle.java
public class Circle implements Shape {
//Declaring instance variable
private double radius;
//Parameterized constructor
public Circle(double radius) {
super();
this.radius = radius;
}
//Getters and setters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//This method will calculate and return the area of the circle
@Override
public double area() {
return Math.PI*radius*radius;
}
}
___________________
Rectangle.java
public class Rectangle implements Shape {
//Declaring instance variables
private double length;
private double width;
//Parameterized constructor
public Rectangle(double length, double width) {
super();
this.length = length;
this.width = width;
}
//Setters and getters
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
//This method will calculate and return the area of the rectangle
@Override
public double area() {
return length*width;
}
}
____________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating the Circle class object by passing the value as arguments
Circle c=new Circle(4);
//creating the Rectangle class object by passing the values as arguments
Rectangle r=new Rectangle(4,3);
//calling the method
showArea(c);
showArea(r);
}
private static void showArea(Shape s) {
double area=s.area();
//displaying the area
System.out.println(\"The area of the shape is \" + area);
}
}
______________________
Output:
The area of the shape is 50.26548245743669
The area of the shape is 12.0
_______thank You


