2, (10 points) Given the Python class below, pertorm the folloving tasks: a. Nam
ID: 3601103 • Letter: 2
Question
2, (10 points) Given the Python class below, pertorm the folloving tasks: a. Name all mutator methods b. Give the output e. Give code to create a sibling class to Rectangle named RightTriangle d. Give code to create a RightTriangle, RT, class Shape: def init_(self, width, height): self.w- width self,h = height def setwidth(self, width): self.w-width def setHeight (self, height): self.h- height class Rectangle (Shape): def name (self): return "rectangle" def area (self): return (self.w self.h) def main): r Rectangle (3, 6) r.setwidth (4) print ('The 'r.name ()+has an area of str(r.area())+ '.' main()Explanation / Answer
# your code goes here
class Shape:
def __init__(self,width,height):
self.w = width
self.h = height
def setWidth(self,width):
self.w = width
def setHeight(self,height):
self.h = height
class Rectangle(Shape):
def name(self):
return "rectangle"
def area(self):
return (self.w * self.h)
class RightTriangle(Shape):
def name(self):
return "right triangle"
def area(self):
return int(1/2 *self.w * self.h)
def main():
r = Rectangle(3,6)
r.setWidth(4)
print('The '+r.name()+' has an area of '+ str(r.area())+'.')
RT = RightTriangle(3,6)
print('The '+RT.name()+' has an area of '+ str(RT.area())+'.')
main()
1. Mutator Methods: The methods which change the state of variable
So we have setWidth and setHeight as the Mutator Methods
2. Output:
Code for RightTriangle class
class RightTriangle(Shape):
def name(self):
return "right triangle"
def area(self):
return int(1/2 *self.w * self.h)
4. Code for creating a RightTriangle object RT
RT = RightTriangle(3,6)
print('The '+RT.name()+' has an area of '+ str(RT.area())+'.')
//This would print the area and name as:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.