Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can I get this in python. Please don't use another expert code. I have also attached the given information code. Given information code: Topics: class,
Can I get this in python. Please don't use another expert code. I have also attached the given information code.
Given information code:
Topics: class, object, instantiation, basic inheritance Problem Statement: The purpose of this lab assignment is to gain experience in python's inheritance feature. Lab Scenario: 1. You are given the definition of a base class named as Polygon. A polygon is a closed figure with 3 or more sides. 2. You are going to implement the child class which is class Triangle. Triangle class is the subclass/child class of superclass Polygon, since triangle is a polygon with 3 sides. 3. The parent class has three methods, already provided in the template file. They are: a. __init__(self, total_sides): this method takes total number of sides as parameter and initializes two variables. total_sides and a list named sides which would be equal to the length of the sides of a polygon. For example: for a triangle object total_sides = 3 and the sides list would be initialized to [0, 0, 0]. b. set_sides(self): this method takes the user input for the sides of the polygon. For example: in case of triangle, it will prompt for three times to take the user input. If you call the method then the sample run can be as follows: Enter side 1:2 Enter side 2:3 Enter side 3:4 Blue represents the code output and black color is the user input. c. get_sides(self): this method works as getter function, which will print the sides of the polygon after setting the sides with set_sides method. For triangle: if you invoke the method then the following will be printed for the above input. Side 1: 2.0 Side 2: 3.0 Side 3: 4.0 4. Your task: You need to define a main() function and considering the definition of the parent class you need to define two methods in Triangle class: __init_and get_area. Please see below: a. main() function: A sample main steps can be as follows: i. Create a triangle object ii. Invoke set_sides() to get user input iii. Invoke get_sides () to print the sides of the triangle object iv. Invoke get_areal) to print the area for the triangle b. _init__(self): which will initialize the super class __init_method with total number of sides to be 3. You can choose to use super() to call the super class method. c. get_area(self): calculates and then prints the area from the three sides of a triangle. i. FYI, Subclass will have access to all the methods and attributes of the parent class ii. Think of how to obtain the three sides from the sides list of the superclass iii. To calculate the area from three sides, you need to implement Heron's formula: 1. If the sides of the triangle are 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 Lab Procedure: 1. Prior starting of the work consider what would be the parameter and return of each methods. 2. Determine the steps to translate the code into the python class syntax. 3. Test the application by giving some user input. Sample input/output: Enter side 1:2 Enter side 2:3 Enter side 3:4 Side 1: 2.0 Side 2: 3.0 Side 3: 4.0 The area of the triangle is: 2.9047375096555625 import math class Polygon (object): definit__(self, total_sides): self.total_sides = total_sides self.sides = [0 for i in range (total_sides)] #initialize sides list def set_sides (self): for i in range (self.total_sides): self.sides[i]= float (input ("Enter side " + str(i + 1) + ": ")) #version 2: no need of conversion def get_sides (self): for i in range (self.total_sides): print ("Side ", str(i + 1), ": ", self.sides[i]) #Define your Triangle class hereStep 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