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

For this recitation you will creating a class called Shape and add a init method

ID: 3779241 • Letter: F

Question

For this recitation you will creating a class called Shape and add a init method to initialize all the variables to zero, which you will be using in the program. Then add methods to the class called rectangle, triangle and circle which will be used to calculate the areas. These methods should take the necessary parameters to calculate the area and print the area. For circle the parameter should be radius. For triangle, the parameter should be height and base. For rectangle, the parameter should be length and width. Name your file Lastname_Firstname.py

Explanation / Answer

class shape(obj):
def __init__(self,w=0,h=0,r=0,s1=0,s2=0,s3=0,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 rectangle(shape):  
def __init__(self,w=1,h=1,t="rectangle"):
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 triangle(rectangle):
def __init__(self,s1=1,s2=1,s3=1,t="triangle"):
def area(self):
"""Heron's Formula"""
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 circle(shape):
def __init__(self,r = 1,t = "circle"):
   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
   def demo():
s = shape()
print "s prints as", s, " "
   t = triangle(2,2,3)
print "t prints as", t
print "perimeter of t is %.2f"%t.perimeter()
print "area of t is %.2f"%t.area(), " "
   r = rectangle(2,3)
print "r prints as", r
print "perimeter of r is %.2f"%r.perimeter()
print "area of r is %.2f"%r.area(), " "
   c = circle()
print "c prints as", c
print "perimeter of c is %.2f"%c.perimeter()
print "area of c is %.2f"%c.area(), " "
  

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