Practice · 2 of 3
Sealed Shapes, Exhaustive Area
Inside Solution, model shapes with a sealed hierarchy and write an
exhaustive area function:
- sealed interface Shape permits Circle, Rect
- record Circle(double radius) implements Shape
- record Rect(double w, double h) implements Shape
- static double area(Shape s) using a pattern-matching switch with a
case per variant (no default branch).
The tests verify both areas and the exhaustive dispatch.
Difficulty: intermediate
Press Submit to check your solution.
Back to lesson: Practice: Design Lab: Payments & Shapes