In: Computer Science
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and pentagon, has some common properties, ex. whether it is filled or not, the color of the shape, and number of sides. Some properties like the area of the shapes are determined differently, for example, area of the rectangle is length * widthand area of a circle is ??2. Create the base class and subclasses to represent the shapes. Create a separate test module where instances of the class are created, and the methods are tested by giving the required details and printing the details for each class
I did Already half of the question can you please complete on it
cause this is what is required:(The Circle Test Code didn't work
please redo the Circle and complete the question requirements:
class Shape: def __init__(self,color,fn,no_of_sides): self.color=color self.filledornot=fn self.no_of_sides=no_of_sides self.sides = [] def inputSides(self): for i in range(self.no_of_sides): self.sides.append(float(input("Enter side " +str(i+1)+" : "))) def display(self): print("color: " , self.color) print("Filled: ", self.filledornot) for i in range(self.no_of_sides): print("Side ",i+1,"is", self.sides[i]) def area(self): print("Area") class Rectangle(Shape): def __init__(self): Shape.__init__(self,"Red",True,2) def area(self): l , w = self.sides calarea=l*w print("Area=" , calarea , "cm**2") class Circle(Shape): '''Class to represent the circle shape''' # constructor def _init_(self): Shape.__init__(self, "White", True, 0) # area function for the circle def area(self): radius = self.sides[0] area_circle = 3.14*radius*radius print("Area= ", area_circle) #Test Code print('Rectangle') r = Rectangle() r.inputSides() print('-'*30) r.display() r.area() print('-'*30) print('Circle') c = Circle() c.inputSides() print('-'*30) c.display() c.area() print('-'*30)
First error in the cirlce class was that the contructor must be
__init__ > double underscore , but you used a single
underscrore,
Second was the number of side , circle must sedn one side(for
radius)
Code
class Shape:
def __init__(self, color, fn, no_of_sides):
self.color = color
self.filledornot = fn
self.no_of_sides = no_of_sides
self.sides = []
def inputSides(self):
for i in range(self.no_of_sides):
self.sides.append(float(input("Enter side " + str(i+1)+" : ")))
def display(self):
print("color: ", self.color)
print("Filled: ", self.filledornot)
for i in range(self.no_of_sides):
print("Side ", i+1, "is", self.sides[i])
def area(self):
print("Area")
class Rectangle(Shape):
def __init__(self):
Shape.__init__(self, "Red", True, 2)
def area(self):
l, w = self.sides
calarea = l*w
print("Area=", calarea, "cm**2")
class Circle(Shape):
'''Class to represent the circle shape'''
# constructor
def __init__(self):
Shape.__init__(self, "White", True, 1)
# area function for the circle
def area(self):
radius = self.sides[0]
area_circle = 3.14*radius*radius
print("Area= ", area_circle, "cm**2")
# Test Code
print('Rectangle')
r = Rectangle()
r.inputSides()
print('-'*30)
r.display()
r.area()
print('-'*30)
print('Circle')
c = Circle()
c.inputSides()
print('-'*30)
c.display()
c.area()
print('-'*30)
Screenshot
Output