← Back to Exam Panel
Problem Statement: Figure & Circle
Your task is to model a **Circle** using an **Abstract Class** and an **Interface**. This problem is designed to help you understand how to use **interfaces** to define common behavior and how to use **abstract classes** to provide a common base for related objects.
Functional Requirements
- Common Behavior: All geometric figures must have methods to calculate their **area** and **perimeter**.
- Circle: You must create a class for a **Circle** that has a `radius`. It should correctly calculate its area and perimeter.
Technical Requirements
Follow this class structure for your solution:
- 1. Create an Interface:
- Create an interface named
Shape with two methods: double getArea() and double getPerimeter().
- 2. Create an Abstract Class:
- Create an abstract class named
Figure that **implements** the Shape interface.
- This class will act as a common base for all figures. You can leave the methods from the `Shape` interface as abstract in this class.
- 3. Create a Concrete Class:
- Create a concrete class named
Circle that **extends** the `Figure` abstract class.
- The
Circle class must have a field for `radius` and provide concrete implementations for both getArea() and getPerimeter().
- 4. Demonstrate Polymorphism:
- In your
main method, create a List of type Shape.
- Add your
Circle object to this list.
- Iterate through the list and call the methods on each object to demonstrate how Java correctly calls the `Circle` class's methods at runtime.
Testing Requirements
Your main method must demonstrate the program works by testing the following scenarios:
- Individual Circle Test:
- Create a
Circle object with a radius of 5.
- Print its calculated area (approx. 78.5) and perimeter (approx. 31.4).
- Polymorphism Test:
- Create a
List and add the Circle object to it.
- Write a `for` loop to iterate through the list. Inside the loop, print the area and perimeter of the shape. The output should show the correct values, proving that the correct method is called on the object.