Shape Area Calculator in Java: Explanation & Practice
Calculate areas of different shapes
Problem summary
Create Shape hierarchy with Rectangle, Triangle, Circle. Each implements area().
Starter code
// Abstract Shape with abstract area()
// Rectangle: width * height
// Triangle: 0.5 * base * height
// Circle: PI * r * r
public class Main {
public static void main(String[] args) {
// Test case 1: Rectangle 4x5
// Print: Area: <result>
}
}Expected output and test cases
- Rectangle 4x5
Area: 20.0
- Triangle base 3, height 4
Area: 6.0
- Circle radius 3
Area: 28.27
Hints
- Each shape stores its own dimensions
- Override area() in each subclass
- Use Math.PI for circle
Related Inheritance exercises
- Practice Polymorphism Demo in Java
- Practice Employee Hierarchy in Java
- Practice Payment Processing System in Java
Practice all Inheritance exercises · Run this idea in the Java compiler