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

  1. Each shape stores its own dimensions
  2. Override area() in each subclass
  3. Use Math.PI for circle

Related Inheritance exercises

Practice all Inheritance exercises · Run this idea in the Java compiler