Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Python this is what I got for part 1 - Implement a class Polygon that abst

ID: 3763406 • Letter: U

Question

Using Python this is what I got for part 1 - Implement a class Polygon that abstracts regular polygons and supports class methdos:

class Polygon:

def __init__(self, n=0, s=0):
        self.length = s
        self.sides = n

    def perimeter(self):
  return self.length * self.sides

    def area(self):
  return (self.length ** 2 * self.sides) / (4 * math.tan(math.pi / self.sides))

I need help on part 2

Implement classes Square and Triangle as subclasses of class Polygon. Each will overload the constructor method __init__ so it takes only one argu- ment l (the side length), and each will support an additional method area() that returns the area of the n-gon object. The method __init__ should make use of the superclass __init__ method, so no instance variables (l and n) are defined in subclasses. Note: The area of an equilateral triangle of side length s is s2 3/2

Explanation / Answer

class shape(object):
def __init__(self,t="shape"):
print "In shape __init__:"
self.tag = t
def __str__(self):
print "In shape __str__:"
return self.tag
def __repr__(self):
print "In shape __repr__: "
return self.__str__()
class polygon(shape):
def __init__(self,ss,t="polygon"):
"""create polygon, ss gives lengths of sides (at least 3 sides)"""
print "In polygon __init__"
shape.__init__(self,t)
self.sides = ss
def __str__(self):
print "In polygon __str__"
res = shape.__str__(self) +", side lengths: "+" ".join([str(l) for l in self.sides])
return res
def perimeter(self):
print "In polygon perimeter"
return sum(self.sides)
class triangle(polygon):
def __init__(self,s1=1,s2=1,s3=1,t="triangle"):
"""create rectangle with sidelengths s1, s2, and s3"""
polygon.__init__(self, [s1,s2,s3], t)
def area(self):
print "In triangle area"
semip = sum(self.sides)/2.0
prod = semip*(semip-self.sides[0])*(semip-self.sides[1])*(semip-self.sides[2])
return math.sqrt(prod)
class rectangle(polygon):
def __init__(self,w=1,h=1,t="rectangle"):
polygon.__init__(self, [w,h], t)
def __str__(self):
print "in rectangle __str__"
res = polygon.__str__(self)+", width: "+str(self.sides[0])+", height: "+str(self.sides[1])
return res
def area(self):
print "in rectangle area"
return self.sides[0]*self.sides[1]
## def perimeter(self):
## print "in rectangle perimeter"
## return 2*(self.sides[0]+self.sides[1])
class square(rectangle):
def __init__(self,l=1,t="square"):
rectangle.__init__(self,l,l,t)
class circle(shape):
def __init__(self,r = 1,t = "circle"):
shape.__init__(self,t)
self.radius = r
def __str__(self):
res = shape.__str__(self) +", radius: "+"%.2f"%self.radius
return res
def perimeter(self):
return 2*math.pi*self.radius
def area(self):
return math.pi*self.radius*self.radius

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote