Create an interface named Shape with the following method si

Create an interface named Shape with the following method signature: double area Next define classes which implement this interface as follows Circle, with instance data for the radius, a constructor which sets the radius and appropriate getter/setter methods Rectangle, with instance data for the length and width, a constructor which sets both properties and appropriate getter/setter methods Use the following code to test your implementations public static void main (String args) Circle c new Circle (4); ll Radius of 4 Rectangle r new Rectangle (4, 3); ll Height 4, Width 3 ShowArea (c) ShowArea (r) public static void ShowArea (Shape s) double area S. area. System. out.println(\"The area of the shape is area

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

 Create an interface named Shape with the following method signature: double area Next define classes which implement this interface as follows Circle, with ins
 Create an interface named Shape with the following method signature: double area Next define classes which implement this interface as follows Circle, with ins
 Create an interface named Shape with the following method signature: double area Next define classes which implement this interface as follows Circle, with ins

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site