logo CodeStepByStep logo

Rectangle

Language/Type: Python classes

Write a class named Rectangle where each object represents a rectangular two-dimensional region. Your class should have the following public 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.
rect.x,
rect.y,
rect.width,
rect.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]".

You should define the entire class including the class heading, the private data attributes, and the declarations and definitions of all the methods and constructor.

Class: Write a complete Python 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.