Q2) Interface: Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The types of shapes are the following:
- Quadrilateral:
- Square: Perimeter = 4xL, Area = LxL
- Rectangle: Perimeter = 2L+W, Area = LxW
- Circle:
- Circumference = TxDiameter, Area = xD/4
- Triangle (assume right triangle):
- Perimeter = a+b+c
- Area = 0.5x base x height (hint: the base and height are the shorter sides)
Provide an interface called Shape2D that contains the common methods and any constants. Then create the classes Quadrilateral, Circle, and Triangle that implement the interface Shape2D. Class Quadrilateral should be an abstract class, and classes Square and Rectangle will extend the abstract class Quadrilateral. For each concrete class, create a toString method that returns the string representation of each shape's measurements.
Create a test file (driver class called TestShape2D). In the test file, create an object of each 2D shape (i.e. square, rectangle, circle, and triangle) and display their information using the implicit toString method. Use hard code for testing. Test creating objects of Interface type and of the same object type.
Example Execution (user input in green):
Rectangle: L=4, W=3, Perimeter - 14.0, Area - 12.0
Square: L=5, Perimeter - 20.0, Area - 25.0
Circle: D=5, Circumference - 15.70, Area - 19.625
Triangle: a=4, b=2, c=3, Perimeter - 9.0, Area - 3.0