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
import math
class Shape:
def __init__(self, side, color='black', fill=False):
self.__color = color
self.__fill = fill
self.__side = side
def get_color(self):
return self.__color
def set_color(self, color):
self.__color = color
def get_fill(self):
return self.__fill
def set_fill(self, fill):
self.__fill = fill
def get_side(self):
return self.__side
def set_side(self,side):
self.__side = side
class Rectangle(Shape):
def __init__(self, length, breadth,side):
super().__init__(side)
self.__length = length
self.__breadth = breadth
def get_length(self):
return self.__length
def set_length(self, length):
self.__length = length
def get_breadth(self):
return self.__breadth
def set_breadth(self, breadth):
self.__breadth = breadth
def get_area(self):
return self.__length * self.__breadth
def get_perimeter(self):
return 2 * (self.__length + self.__breadth)
class Circle(Shape):
def __init__(self, radius,side):
super().__init__(side)
self.__radius = radius
def get_radius(self):
return self.__radius
def set_radius(self, radius):
self.__radius = radius
def get_area(self):
return math.pi * self.__radius ** 2
def get_perimeter(self):
return 2 * math.pi * self.__radius
r1 = Rectangle(10.5, 2.5,4)
print("Number of sides in rectangle r1:",r1.get_side())
print("Area of rectangle r1:", r1.get_area())
print("Perimeter of rectangle r1:", r1.get_perimeter())
print("Color of rectangle r1:", r1.get_color())
print("Is rectangle r1 filled ? ", r1.get_fill())
r1.set_fill(True)
print("Is rectangle r1 filled ? ", r1.get_fill())
r1.set_color("orange")
print("Color of rectangle r1:", r1.get_color())
c1 = Circle(12,0)
print("\nNumber of sides in circle c1:",c1.get_side())
print("\Area of circle c1:", format(c1.get_area(), "0.2f"))
print("Perimeter of circle c1:", format(c1.get_perimeter(),
"0.2f"))
print("Color of circle c1:", c1.get_color())
print("Is circle c1 filled ? ", c1.get_fill())
c1.set_fill(True)
print("Is circle c1 filled ? ", c1.get_fill())
c1.set_color("blue")
print("Color of circle c1:", c1.get_color())
Output:
Number of sides in rectangle r1: 4 Area of rectangle r1: 26.25 Perimeter of rectangle r1: 26.0 Color of rectangle r1: black Is rectangle r1 filled ? False Is rectangle r1 filled ? True Color of rectangle r1: orange Number of sides in circle c1: 0 \Area of circle c1: 452.39 Perimeter of circle c1: 75.40 Color of circle c1: black Is circle c1 filled ? False Is circle c1 filled ? True Color of circle c1: blue