OOP Fundamentals · Coding problem
Total Area via Polymorphism
A free coding problem from the OOP Pillars & Composition skill. Read it here, then solve it in the app in Java or Python.
- Easy
- Coding problem
- Free
- Java and Python
The problem
You're given a mix of shapes, each one of CIRCLE <radius>, RECTANGLE <width> <height>, or TRIANGLE <base> <height>. Implement each shape's area() so that summing over a List<Shape> - without ever checking which concrete type each one is - gives the correct total area.
Example
Input:
3
CIRCLE 1
RECTANGLE 2 3
TRIANGLE 4 5
Output: 19.14
Print the total, rounded to 2 decimal places.
In Python: Shape is an abstract base class (abc.ABC) whose area() is an @abstractmethod; each shape class overrides it.
Constraints
0 <= number of shapes <= 10000 <= every dimension <= 1000
How it's graded
- Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
- Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
- Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.
More problems in OOP Pillars & Composition
Solve it Total Area via Polymorphism free
No card needed. Lessons, practice questions and easy problems are free forever.
Free plan, no card · 7-day Pro trial · Cancel anytime