Loading...
Loading...
A drawing app keeps all shapes in one Shape[]. Circle extends Shape, Ring extends Circle and Square extends Shape. For each query type, print how many stored objects could be cast to that type without a ClassCastException. Use instanceof; never cast to test.
CIRCLE, RING or SQUARE. Then a line with q, then q lines with a query type: SHAPE, CIRCLE, RING or SQUARE.<TYPE> <count>.Example
Input
4 CIRCLE RING SQUARE RING 4 SHAPE CIRCLE RING SQUARE
Output
SHAPE 4 CIRCLE 3 RING 2 SQUARE 1
A ring IS-A circle, so both rings also count for CIRCLE.
Try the problem first. Open one hint at a time.
Build the objects first: new Circle(), new Ring() or new Square() into the Shape[].
Write static boolean fits(Shape s, String type) that returns s instanceof Circle for CIRCLE, and so on.
A cast to a type works exactly when instanceof that type is true, so counting trues answers the question.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor