Loading...
Loading...
Loading...
A land office stores plots as squares, rectangles and right triangles. Write a parent class Shape with String name() and long area(), and one child class per shape that overrides both. Read every plot into one Shape[] and print each area, then the total.
SQUARE a, RECT l b or TRIANGLE b h.<NAME> <area> for each plot in input order, then Total <sum>. A triangle's area is b × h / 2, rounded down.Example
Input
3 SQUARE 4 RECT 3 5 TRIANGLE 3 5
Output
SQUARE 16 RECT 15 TRIANGLE 7 Total 38
3 × 5 / 2 = 7.5, rounded down to 7.
Try the problem first. Open one hint at a time.
Give Shape a default area() that returns 0; each child overrides it with its own formula.
Read the type word first, then create the right child and store it in the Shape[] array.
One loop over the array prints s.name() + " " + s.area() and adds to a long total.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor