Solution 1

class Shape(object):
    def area(self):
        raise NotImplementedError()


class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * (self.radius ** 2)


class Rectangle(Shape):
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def area(self):
        return self.height * self.width


class Square(Rectangle):
    def __init__(self, side):
        super(Square, self).__init__(side, side)

Shape Areas

Complete the given shape classes to compute the correct area for all of them:

Circles

Are created by passing radius in the constructor. The area of a circle is equals to πr^2. That's 3.14 * radius * radius (we round pi to 3.14).

Rectangles

They're created by passing height and width and area is equals to height * width.

Squares

Squares are created by passing the side measure of the square and the area is side * side .

IMPORTANT Squares are subclasses of Rectangles. The Square class should NOT implement the area method, only the __init__ method.

Examples:

c = Circle(radius=10)
c.area()  # 314.0 (3.14 * 10 * 10)

s = Square(side=4)
s.area()  # 16 (4 * 4)

r = Rectangle(height=4, width=3)
r.area()  # 12 (4 * 3)

Test Cases

test square - Run Test

def test_square():
    s = Square(side=4)
    assert s.area() == 16

test circle - Run Test

def test_circle():
    c = Circle(radius=10)
    assert c.area() == 314.0

test rectangle - Run Test

def test_rectangle():
    r = Rectangle(height=4, width=3)
    assert r.area() == 12
class Shape(object): def area(self): raise NotImplementedError() class Circle(Shape): pass class Rectangle(Shape): pass class Square(Rectangle): # Only __init__ method pass