Python Question A1b: Rectangle (4 points) Complete all the methods in the class
ID: 3896335 • Letter: P
Question
Python Question A1b: Rectangle (4 points)
Complete all the methods in the class Rectangle.
Question A1b: Rectangle (4 points) Complete all the methods in the class Rectangle class Rectangle(Shape): def init_(self, x, y, w, h): """Rectangle at (x, y) with width w and height h""" pass # Replace this Line with your answer def area (self): ""Returns the area of the rectangle"n pass # Replace this line with your answer ?sSquare(self): "" "Returns True if it is a square otherwise return False""" pass # Replace this line with your answer def def perimeter(self): """Returns the perimeter of the rectangle""" pass # Replace this line with your answer def str(self): return "Rectangle at ("str(self.x) ", "+ str(self.y) "), width "str(self.w) +", height "str (self.h) r-Rectangle(e,0, 10, 10) r.move (1, 1) assert ra()-100 assert r.isSquare()True assert r.perimeter()-40Explanation / Answer
class rectangle():
def __init__(self,x,y,w,h):
self.x=x
self.y=y
self.w=w
self.h=h
def area(self):
#Returns the area of Rectangle
return self.w*self.h
def isSquare(self):
"""Returns true if Rectangle is squre else false"""
return self.w==self.h
def perimeter(self):
"""Returns the Perimeter of Rectangle"""
return (2*self.w)+(2*self.h)
def __str__(self):
return "Rectangle at ("+str(self.x)+","+str(self.y)+"), width "
+str(self.w)+", height "+str(self.h)+"."
r=rectangle(0,0,10,10)
#r.move(1,1) this is not valid because move() attribute is part of rectangle class
assert r.area()==100
assert r.isSquare()==True
assert r.perimeter()==40
def __init__(self,x,y,w,h): In this method initialize self object (you can choose any appropriate name and used that in every function ) with parameter passed
area() : In this function we use the formula of area i.e area=width * height and return value
isSquare(): If the w and h variable are equal than only it returns true
perimeter() : In this function we use the formula i.e Perimeter =2width +2height and return value
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.