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

Use python please Create a class named \'Rectangle\' with the following attribut

ID: 3765972 • Letter: U

Question

Use python please

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)

part2

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:

      x=0

      y=0

      width=0

      height=0

      def __init__(self, x, y, width, height):

                   self.x = x

                   self.y = y

                   self.width=width

                   self.height=height

      def complete(self):

                   return self.x,self.y,self.width,self.height

      def right(self):

                   return self.x + self.width

      def __str__(self):

                   return('Rectangle(' + str(self.x) + ',' + str(self.y) + ',' + str(self.width) + ',' + str(self.height)+')')

      def bottom(self):

                   return self.y + self.height

     def __str__(self):

                   return('Rectangle(' + str(self.y) + ',' + str(self.height) + ',' + str(self.y) + ',' + str(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):

                   ' '

     print(Rectangle(6,12,40,40).complete())

      print(Rectangle(3, 5, 10, 20).right())

     print(Rectangle(12, 10, 72, 35).right())