HomeChallengesRepsProblemsLeaderboardTake a challenge

One caller, many shapes

A switch that must be edited for every new type. Let the objects answer instead.

Intermediate15 min on the clock4 graded checks · 100 pointsObjects That Behave
Graded by running your code against real cases.

What you build

  • Declare an interface with an area() method
  • Give each shape its own class implementing it
  • Rewrite totalArea so it never asks what kind of shape it has
  • Make a shape you have never seen work without touching totalArea

Done means

totalArea works for the three known shapes and for one it has never seen.

How it is graded

Published in full, before you start — every point is one of these and there is nothing else. Each one runs your code; it is not a search for keywords.

  1. Each shape computes its own area+25Let the object answer
  2. totalArea adds them up without asking what they are+25Let the object answer
  3. A shape it has never seen works+30Let the object answer
  4. Every shape satisfies one contract+20Let the object answer

What it teaches

The rest of Objects That Behave