logo CodeStepByStep logo


Language/Type: Python classes
Related Links:

Add a method named intersection to the Rectangle class. Use your Rectangle class solution from the previous exercises 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.
data attributes private attributes storing rectangle coordinates
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 a method named intersection to your class. Your method should accept another rectangle as a parameter and return a new rectangle that represents the largest rectangular region completely contained within both the current rectangle (self) and the given other rectangle. If the rectangles do not intersect at all, return a rectangle with its width and height both equal to 0.

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.