Loading...
Loading...
A plot planner works with square, rectangular and triangular plots. A plain "shape" has no area formula, so it must never be created.
Write an abstract class Shape with abstract String name(), abstract double area() and a concrete String describe() that returns name area. Write Square, Rectangle and Triangle. After all plots, print the largest one.
square s, rect w h or tri b h.Name area (area as a Java double, like 24.0), then Largest: Name area (the first one on a tie).Example
Input
3 rect 6 4 square 5 tri 10 6
Output
Rectangle 24.0 Square 25.0 Triangle 30.0 Largest: Triangle 30.0
The triangle has the largest area: 10 × 6 / 2 = 30.
Try the problem first. Open one hint at a time.
describe() is written once in Shape and calls the abstract name() and area().
For the triangle, divide by 2.0, not 2, so 7.5 is not cut to 7.
Keep a Shape largest variable and replace it only when an area is strictly bigger.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor