Question: Python Question3 &c 07 AV I Exam Question2.py Question1.py Question2.py Question3.py X 4 Closely follow these instructions to create an inheritance structure to represent some


Python
Question3 &c 07 AV I Exam Question2.py Question1.py Question2.py Question3.py X 4 Closely follow these instructions to create an inheritance structure to represent some 2D shapes. 5 6 Create a class called Shape. It should have one private instance variable of type integer and 7 named number_of_sides. It should have a constructor that takes one argument, the number of sides. 8 Instantiate the number_of_sides instance variable in the constructor body. Create a method named 9 calculate_area that takes no arguments and just passes (no implementation). Create another method named 0 calculate_perimeter that takes no arguments and just passes (no implementation). 1. Create appropriate getters and setters. 2 3 Create a class named Rectangle that is a child of Shape. It should have two private instance variables, 4 a float named width and a float named height. Create a constructor that takes three arguments, 5 number_of_sides, width, and height. Fulfill the constructor requirement of the parent class. L6 Implement the calculate_area method. 7 - Area of a rectangle is calculated as: area = width * height 8 Implement the calculate_perimeter method. 9 - Perimeter of a rectangle is calculated as: perimeter = 2(width) + 2(height) LO Create appropriate getters and setters. Create an appropriate -_eq-_ method. 1 Create a __str__ method to return a string in the following format (where x is the appropriate value): 2 "A rectangle has x sides / Area = x / Perimeter = x" 3 4 Create a class named Circle that is a child of Shape. It should have one private instance variable, a float 5 named radius. Create a constructor that takes two arguments, number_of_sides, and radius. Fulfill the L6 constructor requirement of the parent class. 7 Implement the calculate_area method. 8 - Area of a circle is calculated as: area = PI * radius^2 9 Implement the calculate_perimeter method. Perimeter (circumference) of a circle is calculated as: perimeter = 2 * PI * radius Create appropriate getters and setters. Create an appropriate -_eq-_ method. Create a __str__ method to return a string in the following format (where x is the appropriate value): "A circle has x sides | Area = x / Perimeter = x" Create a class named Equilateral Triangle that is a child of shape. It should have one private instance variable, a float named side_length. Create a constructor that takes two arguments, number_of_sides, and side_length. Fulfill the constructor requirement of the parent class. Implement the calculate_area method. Area of an equilateral triangle is calculated as: area = side_length^2 * sqrt(3) / 4 Or by using angles! Implement the calculate_perimeter method. Perimeter of an equilateral triangle is calculated as: perimeter = side_length * number_of_sides coate appropriate getters and setters. Create an appropriate __eq__ method. Create a __str__ method to return a string in the following format (where x is the appropriate value) :| "A circle has x sides / Area = x / Perimeter Jif __name__ == '__main__': rectangle = Rectangle(4, 4.1, 2.3) print(rectangle) # 4 sides, area=9.43, perimeter=12.8 circle = Circle(1, 3.5) print(circle # 1 side, area=38.48, perimeter=21.99 equilateralTriangle = EquilateralTriangle(3, 2.8) print(equilateralTriangle) # 3 sides, area=3.39, perimeter=8.40 print() # Now to test your polymorphism! shapes = [ Circle(1, 3.2), # 1 side, area=32.17, perimeter=20.11 Rectangle(4, 2, 2.6), # 4 sides, area=5.20, perimeter=9.20 Circle(1, 8.3), # 1 side, area=216.42, perimeter=52.15 EquilateralTriangle(3, 9.1) # 3 sides, area=35.86, perimeter=27.30 ] for shape in shapes: print(shape)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
