logo CodeStepByStep logo

Rectangle_contains

Language/Type: Python classes
Related Links:

Add a method named contains to the Rectangle class. Use your Rectangle class solution from the previous exercise as a starting point to solve this exercise.

Suppose a class named Rectangle has already been created that has the following members:

member name type description
Rectangle(x, y, w, h) constructor Initializes a new rectangle whose top-left corner is specified by the given (x, y) coordinates and with the given width and height, w by h. Raises a ValueError on a negative width or height.
ba._x,
ba._y,
ba._width,
ba._height
data attributes private attributes storing rectangle coordinates
ba.x,
ba.y,
ba.width,
ba.height
property the rectangle location and dimensions (read-only)
str(r) method returns a string representation of the rectangle, such as "Rectangle[x=1,y=2,width=3,height=4]".

Add an accessor method named contains to your class. Your method should accept two integers representing x/y coordinates as parameters and return True if the given coordinates lie inside the bounds of the rectangle.

Member function: Write a member function that will become part of an existing class. You do not need to write the complete class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.