Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Sample Input and Output What to solve in python CODE in python Need help finishing the code Object creation: Create a three objects s, r,
Sample Input and Output
What to solve in python
CODE in python
Need help finishing the code
Object creation: Create a three objects s, r, t which will initialize sides by taking user input. Print the object s,r, Sample input/output: Creating the parent class This is a Square. Please enter 1 sides of interest Enter side 1: >? 4 This is a Square. Sides are: 4.0 Perimeter: 16.0 Area: 16.0 Creating the child 1 This is a Rectangle. Please enter 2 sides of interest Enter side 1: >? 5 Enter side 2: >? 6 This is a Rectangle. Sides are: 5.9,6.0 Perimeter: 22.0 Area: 30.0 Creating the child 2 This is a Triangle. Please enter 3 sides of interest Enter side 1: >? 6 Enter side 2: >? 7 Enter side 3: >? 8 This is a Triangle. Sides are: 6.0, 7.,8.0 Perimeter: 21.0 Area: 20.33316256758894 You are given the definition of a base class named as Polygon. A polygon is a closed figure with three or more sides. The base class is a Square. It will calculate the perimeter and area of a Square. You are going to implement the child classes which are class Rectangle and Triangle. a. Rectangle class is the subclass/child class of superclass Polygon, since rectangle is a polygon with 4 sides. b. Triangle class is the subclass/child class of superclass Polygon, since triangle is a polygon with 3 sides. The parent class has four methods, already provided in the template file. They are: Your task: You need to modify three methods in Rectangle and Triangle class: __init__(),get_perimeter(), get_areal) and __str_0). Please see below: a. __init__(self): which will call the super class_init_method. Since all the triangle has three sides of interest, you need to define a class variable sides_of_interest and set it to 3. Use class variable as a parameter to super() function call. In case of Rectangle sides of interest would be two (length and breadth) b. get_perimeter(self): returns the perimeter from the sides of interest. For rectangle 2*(length+breadth) and for triangle (a+b+c) and C. get_area(self): returns the area from the three sides of polygon. i. Think of how to obtain the three sides from the sides list of the superclass. Hint: Subclass will have access to all the methods and attributes of the parent class ii. To calculate the area from length and breadth of rectangle the area would be: Area = Length x Breadth iii. To calculate the area from three sides of triangle: a, b and c, then the area is given by: Area = P(p-a)(p-b)(p-c) where p is half the perimeter, or ago d. __str_(self): returns the name of the shape, sides, perimeter and area of the polygon. You do not have to modify anything here. import math class Polygon (object): This class is the base class of Polygon. By default it will consider the Polygon as square. def _init__(self, sides_of_interest = 1, shapename = 'Square') : self.sides of interest = sides of interest self.shapename = shapename print ('This is a {}. Please enter {} sides of interest'. format (self.shapename, self.sides_of_interest)) self.sides = [] for i in range (self.sides of interest): self.sides.append(float (input ("Enter side " + str(i + 1) + ": "))) def get perimeter (self): side A = self.sides [0] perimeter = 4+ side_A return perimeter def get area (self): side_A = self.sides [0] area = side Aside A return area def _str_(self): return 'This is a {}. Sides are: {} Perimeter: {} Area: {}'.format (self.shapename, (','.join([str(i) for i in self.sides])), self.get_perimeter(), self.get_area()) class Rectangle (Polygon): sides of interest = # class variable def init__(self): # Your code here def get_perimeter (self): # Your code here def get_area (self): # Your code here class Rectangle (Polygon): sides_of_interest = # class variable definit__(self): # Your code here def get_perimeter (self): # Your code here def get area (self): # Your code here class Triangle (Polygon): sides_of_interest = # class variable definit_(self): # Your code here def get_perimeter (self): # Your code here def get_area (self): # Your code here print ('Creating the parent class') s=Polygon() # Parent print (s) print(" Creating the child i') r = Rectangle() # Child 1 print (r) print(" Creating the child 2') t = Triangle() # Child 2 print (t)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started