John, the fifth standard student wants to build shapes and he collected rectangle, square and circle.
He coloured all the shapes and submitted to his teacher. His teacher wants him to write a program in
python to find the area of all the shapes. Suggest john with object oriented techniques to solve the
problem.
Algorithm:
Define a class name shapes
Create a function area
Define class circle uses class shapes
Use function area
Calculate area= radius **2*3.14
Define class rectangle uses class shapes
Use function area
Calculate area= width * height
Define class triangle uses class shapes
Use function area
Calculate area= base * height/2
Call the class circle, rectangle, triangle by passing arguments
Print area of circle
Print area of rectangle
Print area of triangle
class Shapes:
def area(self):
pass
class Circle(Shapes):
def __init__(self, radius):
self.radius = radius
def area(self):
return self.radius ** 2 * 3.14
class Rectangle(Shapes):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
class Triangle(Shapes):
def __init__(self, base, height):
self.base = base
self.height = height
def area(self):
return self.base * self.height / 2
a = Circle(2)
b = Rectangle(3,4)
c = Triangle(5,6)
print(a.area())
print(b.area())
print(c.area())
output
12.56
12
15.0