I need some help answering these questions. They must be completed using Python
ID: 3697331 • Letter: I
Question
I need some help answering these questions. They must be completed using Python 3.x -
Class Rectangle: (10 points)
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 ``bottom`` 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)
Part 2: (5 points)
Create a method called ``contains_point`` that takes coordinates x and y as parameters and returns True if the point in inside (or on the edge) of the rectangle and False otherwise.
>>> r = Rectangle(30, 40, 100, 110)
>>> r.contains_point(50, 50)
True
>>> r.contains_point(30,40)
True
>>> r.contains_point(130, 150)
True
>>> r.contains_point(131, 50)
False
>>> r.contains_point(0,0)
False
Explanation / Answer
class Rectangle:
def __init__(self,x,y,w,l):
self.x=x
self.y=y
self.w=w
self.l=l
def __str__(self):
a="Rectangle("
a = a+str(self.x)
a = a+','+str(self.y)
a = a+','+str(self.w)
a = a+','+str(self.l)+')'
return a
def right(self):
return self.x+self.w
def bottom(self):
return self.y+self.l
def size(self):
return (self.w,self.l)
def position(self):
return (self.x,self.y)
def area(self):
return self.w*self.l
def expand(self,offset):
r1 = Rectangle(self.x-offset,self.y-offset,self.w+2*offset,self.l+2*offset)
return r1
def contains_point(self,x1,y1):
if(x1-self.x<=self.w and x1-self.x>=0 and y1-self.y<=self.l and y1-self.y>=0):
return True
else:
return False
r = Rectangle(5, 10, 50, 100)
print(r)
r3 = Rectangle(3, 5, 10, 20)
print(r3.right())
print(r3.bottom())
print(r3.size())
print(r3.position())
print(r3.area())
r1 = r.expand(3)
print(r)
print(r1)
r = Rectangle(30, 40, 100, 110)
print(r.contains_point(50, 50))
print(r.contains_point(50, 0))