need to use python 3.5 Create a class named \'Rectangle\' with the following att
ID: 3767365 • Letter: N
Question
need to use python 3.5
Create a class named 'Rectangle' with the following attributes and methods (sample run for each method included):
1) Each instance should have an x, y, width, and height attributes.
2) You should be able to pass the attributes when creating the rectangle as follows, (where x=5, y=10, width=50, height=100 in that order):
r = Rectangle(5, 10, 50, 100)
3) Create a method that returns the rectangle as a string (hint: implement __str__ ). For a rectangle object with attribute values x=5, y=10, width=50, height=100, it should return the string “Rectangle(5, 10, 50, 100)”.
>>> r2 = Rectangle(5, 10, 50, 100)
>>> print(r2)
Rectangle(5, 10, 50, 100)
4) Create a method called ``right`` that gets the value of the right edge of the rectangle. It should take no arguments:
>>> r3 = Rectangle(3, 5, 10, 20)
>>> r3.right()
13
>>> r4 = Rectangle(12, 10, 72, 35)
>>> r4.right()
84
5) Create a method called ``top`` that gets the value of the bottom edge of the rectangle.
>>> r5 = Rectangle(5, 7, 10, 6)
>>> r5.bottom()
13
>>> r5.y += 12
>>> r5.bottom()
25
6) Create a method called ``size`` that returns the width and height of your rectangle.
>>> r6 = Rectangle(1, 2, 3, 4)
>>> r6.size()
(3, 4)
7) Create a method called ``position`` that returns the x and y coordinates of your rectangle.
>>> r6.position()
(1, 2)
8) Create a method called ``area`` that returns the area of your rectangle.
>>> r6.area()
12
9) Create a method called ``expand`` that takes an offset value and returns a copy of the rectangle expanded with offset in all directions.
>>> r = Rectangle(30, 40, 100, 110)
>>> r
Rectangle(30, 40, 100, 110)
>>> r1 = r.expand(offset=3)
>>> r1
Rectangle(27, 37, 106, 116)
The original rectangle should not be modified.
>>> r
Rectangle(30, 40, 100, 110)
Negative values should return a shrunken rectangle.
>>> r.expand(-5)
Rectangle(35, 45, 90, 100)
Explanation / Answer
class Rectangle:
x = 0
y = 0
width = 0
height = 0
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.width = w
self.height = h
def __str__(self):
return "Rectangle(%d, %d, %d, %d)" % (self.x, self.y, self.width, self.height)
def right(self):
return self.x + self.width
def bottom(self):
return self.y + self.height
def size(self):
return (self.width, self.height)
def position(self):
return (self.x, self.y)
def area(self):
return self.width * self.height
def expand(self, offset):
return Rectangle(self.x - offset, self.y - offset, self.width + 2 * offset, self.height + 2 * offset)
def contains_point(self, x, y):
return (x >= self.x and x <= self.x + self.width and y >= self.y and y <= self.y + self.height)
if __name__ == '__main__':
# test code
r2 = Rectangle(5, 10, 50, 100)
print (r2)
r3 = Rectangle(3, 5, 10, 20)
print (r3.right())
r4 = Rectangle(12, 10, 72, 35)
print (r4.right())
r5 = Rectangle(5, 7, 10, 6)
print (r5.bottom())
r5.y += 12
print (r5.bottom())
r6 = Rectangle(1, 2, 3, 4)
print (r6.size())
print (r6.position())
print (r6.area())
r = Rectangle(30, 40, 100, 110)
print (r)
r1 = r.expand(offset = 3)
print (r1)
print (r)
print (r.expand(-5))
r = Rectangle(30, 40, 100, 110)
print (r.contains_point(50, 50))
print (r.contains_point(30, 40))
print (r.contains_point(130, 150))
print (r.contains_point(131, 50))
print (r.contains_point(0, 0))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.