Question: Need help finishing this, please!! Topics: class, object, instantiation, basic inheritance, polymorphism, Abstract Base Class Problem Statement: The purpose of this lab assignment is to
Need help finishing this, please!!



Topics: class, object, instantiation, basic inheritance, polymorphism, Abstract Base Class Problem Statement: The purpose of this lab assignment is to gain experience in python's polymorphism feature. Lab Scenario: 1. You are given the definition of a base class named as Polygon. A polygon is a closed figure with three or more sides. Class Polygon is an Abstract Base Class since it contains two abstract methods. 2. You are going to implement the child classes which are class Square and Triangle. a. Square class is the subclass/child class of superclass Polygon, since Square is a polygon with 4 sides, where each side is equal. b. Triangle class is the subclass/child class of superclass Polygon, since triangle is a polygon with 3 sides. Both these subclasses have same named method; hence they are polymorphic. 3. The parent class has three methods, already provided in the template file. They are: C. Method name _init__(self, side_of_interest=", shapename=") three Description Parameter Return This method takes total number of sides sides_of_interest None of interest and shapename as parameters shapename and initializes variables: shapename, sides_of_interest and a list of sides. For a square object sides_of_interest = 1 and 'sides' list would be initialized by taking user input. For example: in case of square it will prompt for only one time to take the user input. For triangle_init_ will take three sides information This is an abstract method and subclass None None needs to implement the method. perimeter(self) area(self) None This is an abstract method and subclass None needs to implement the method. 4. Your task: You need to modify three methods in Square and Triangle class: _init_0), perimeter(), and area(). 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 Square sides of interest would be 1 since all sides are equal. b. perimeter(self): returns the perimeter from the sides of interest. For square perimeter is (4 * length of a side) and for triangle it is summation of three sides (a+b+c). c. area(self): returns the area from the 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. Subclass will have access to sides list as initialized in super class. ii. Area of a square is: Area = (side)^2 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) a+b+c where p is half the perimeter, or 2 Object creation: Create two objects triangle and Square object which will initialize sides by taking user input. Print the object area and perimeter. Sample input/output: Creating child 1 Enter sides of Triangle Enter side 1: 2 Enter side 2:4 Enter side 3:5 Perimeter: 11.00 Area: 3.80 Creating child 2 Enter sides of Square Enter side 1: 10 Perimeter: 40.00 Area: 100.00 import math class Polygon (object): #an ABC class This class is the base class of Polygon. By default it will consider the Polygon as square. def __init__(self, side_of_interest='', shapename=''): self.side_of_interest = side_of_interest #initialize side of interest self.shapename = shapename print("Enter sides of {}'.format(self.shapename)) self.sides = [] #holds all the sides for i in range(self.side_of_interest): #user input self.sides.append(float(input("Enter side " + str(i + 1) + ": "))) def perimeter(self): #abstract base method raise NotImplementedError('Subclass must implement it') def area(self): #abstract base method raise NotImplementedError(Subclass must implement it') class Square (Polygon): side_of_interest = 1 # class variable #define _init_ # init method will call the super init method def __init__(self): super(). __init__(square.side_of_interest, "Square") #define perimeter def perimeter(self): return self.sides[0] * 4 #define area def area (self): return self.sides[0] ** 2 class Triangle(Polygon): side_of_interest = 3 #define _init__and call the super init method def __init__(self): #define perimeter def perimeter(self): #define area def area (self): #create Triangle and Square object
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
