an Inheritance problem for python version 3.5 for mac: Implement classescalled S
ID: 3776434 • Letter: A
Question
an Inheritance problem for python version 3.5 for mac:
Implement classescalled Square and Triangle as subclasses ofclass Polygon.
Eachwill overload the constructormethod __init__ so ittakes only one argumentlength (side length) and eachwill support
an additional method area() thatreturns the area of the n-gon object.The method __init__ should make useof the supercalss __init__ method, so no instance variables (__numberOfSides & __sideLength) are definedin subclasses.
The areaof an equilateral triangle of sidelength s is (s**2)*(sqrt(3))//2
Write a driverclass classed InheritanceDemo.py
Create an object of square with length 2.Display Perimeter and Area ofthe square object
Create an object of Triangle with side 3.Display Perimeter and Area of the Triangle object
the written Polygon.py program:
#Implementaclass Polygonthat abstractsregular polygonsthatcontains two private variables __numOfSides
#and __sideLength.Inaddition tothe usual accessorand mutator methodsfor numberof sides andside length,
#theclass also includes the methods;
#__init__()
#perimeter()
#area()
#__str__()
#Area = ((s**2) * n) / (4 * (tan (pi / n)))
#Where:
# s = length of eachside
#n = number of sides
import sys
import math
class Polygon:
def __init__(self, num_sides, side_length):
self.num_sides = num_sides
self.length = side_length
def area(self):
tan = math.tan
pi = math.pi
ar = ((self.length ** 2) * self.num_sides) / (4 * (tan(pi / self.num_sides)))
return ar
def perimeter(self):
perm = self.num_sides * self.length
return perm
def __str__(self):
return "Polygon"
#def main():
num_sides, side_length = raw_input('Enter the number of sides and length of each side separated by comma: ').split('.')
num_sides, side_length = [int(num_sides), int(side_length)]
polygon1 = Polygon(num_sides, side_length)
print('Number of sides: ', str(num_sides))
print('Length of each side: ', str(side_length))
print('Perimeter = ', str(polygon1.perimeter()))
print('Area = ', str(polygon1.area()))
#to test __str__, use print polygon1
Explanation / Answer
import math
class Polygon:
def __init__(self, num_sides, side_length):
self.num_sides = num_sides
self.length = side_length
def area(self):
tan = math.tan
pi = math.pi
ar = ((self.length ** 2) * self.num_sides) / (4 * (tan(pi / self.num_sides)))
return ar
def perimeter(self):
perm = self.num_sides * self.length
return perm
def __str__(self):
return "Polygon"
class Square(Polygon):
def __init__(self,side_length):
Polygon.__init__(self,4,side_length)
def area(self):
ar = (self.length**2)
return ar
class Triangle(Polygon):
def __init__(self,side_length):
Polygon.__init__(self,3,side_length)
def area(self):
ar = (self.length**2)*(math.sqrt(self.num_sides))//2
return ar
num_sides, side_length = input('Enter the number of sides and length of each side separated by comma:').split(',')
num_sides, side_length = [int(num_sides), int(side_length)]
polygon1 = Polygon(num_sides, side_length)
print('Number of sides: ', str(num_sides))
print('Length of each side: ', str(side_length))
print('Perimeter = ', str(polygon1.perimeter()))
print('Area = ', str(polygon1.area()))
#to test __str__, use print polygon1
square = Square(2);
print("-----Square------")
print("Perimeter = ",str(square.perimeter()))
print("Area: ",str(square.area()));
triangle = Triangle(3);
print("-----Triangle------")
print("Perimeter = ",str(triangle.perimeter()))
print("Area: ",str(triangle.area()));
"""
sample output
Enter the number of sides and length of each side separated by comma: 2,1
Number of sides: 2
Length of each side: 1
Perimeter = 2
Area = 3.061616997868383e-17
-----Square------
Perimeter = 8
Area: 4
-----Triangle------
Perimeter = 9
Area: 7.0
"""
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.