0% found this document useful (0 votes)
6 views

pythonproblem

The document outlines a Python program using object-oriented techniques to calculate the area of different shapes: circle, rectangle, and triangle. It defines a base class 'Shapes' and subclasses for each shape that implement an 'area' method to compute their respective areas. The program creates instances of each shape and prints their calculated areas.

Uploaded by

srini767676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

pythonproblem

The document outlines a Python program using object-oriented techniques to calculate the area of different shapes: circle, rectangle, and triangle. It defines a base class 'Shapes' and subclasses for each shape that implement an 'area' method to compute their respective areas. The program creates instances of each shape and prints their calculated areas.

Uploaded by

srini767676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like