codejump
MediumPro challengePythonJavaScriptTypeScript

OOP — Shape Inheritance

PythonOOPClasses

Build a small class hierarchy, then answer one question about it per call.

solve(aspect) returns { aspect: value } — a single-key dict naming what was
asked. The grader compares JSON, so it asks for one aspect at a time rather than
for the classes themselves.

The hierarchy

Shape (base class)

  • Constructor: __init__(self, color: str)
  • Method: describe(self) -> str returns "I am a {color} shape."

Circle (extends Shape)

  • Constructor: __init__(self, color: str, radius: float)
  • Method: area(self) -> float returns π × radius² (use math.pi)
  • Overrides describe(self) → "I am a {color} circle with radius {radius}."

Rectangle (extends Shape)

  • Constructor: __init__(self, color: str, width: float, height: float)
  • Method: area(self) -> float returns width × height
  • Overrides describe(self) → "I am a {color} rectangle of {width}x{height}."

The aspects

aspectvalue to return
shape_describeShape("blue").describe()
circle_describeCircle("red", 5).describe()
circle_area_approxCircle("red", 5).area() rounded to 2 decimals
circle_area_unitCircle("red", 1).area()
rectangle_describeRectangle("green", 4, 6).describe()
rectangle_areaRectangle("green", 4, 6).area()
circle_is_shapewhether a Circle is an instance of Shape
rectangle_is_shapewhether a Rectangle is an instance of Shape

Example

solve("circle_describe")  # {"circle_describe": "I am a red circle with radius 5."}
solve("rectangle_area")   # {"rectangle_area": 24}

In JavaScript / TypeScript — Use class … extends Shape and super(color); the area is Math.PI * radius ** 2, rounded to two decimals for circle_area_approx, and instanceof answers the *_is_shape aspects.

Sample tests

Test #1Shape base describe
Input: ["shape_describe"]
Output: {"shape_describe":"I am a blue shape."}
Test #2Circle describe override
Input: ["circle_describe"]
Output: {"circle_describe":"I am a red circle with radius 5."}
Test #3Circle area ≈ π×5², rounded
Input: ["circle_area_approx"]
Output: {"circle_area_approx":78.54}
Test #4Rectangle describe override
Input: ["rectangle_describe"]
Output: {"rectangle_describe":"I am a green rectangle of 4x6."}
Test #5Rectangle area 4×6
Input: ["rectangle_area"]
Output: {"rectangle_area":24}