Knowee
Questions
Features
Study Tools

Write a class Rectangle that defines a rectangle by: (based on 1-rectangle.py)Private instance attribute: width:property def width(self): to retrieve itproperty setter def width(self, value): to set it:width must be an integer, otherwise raise a TypeError exception with the message width must be an integerif width is less than 0, raise a ValueError exception with the message width must be >= 0Private instance attribute: height:property def height(self): to retrieve itproperty setter def height(self, value): to set it:height must be an integer, otherwise raise a TypeError exception with the message height must be an integerif height is less than 0, raise a ValueError exception with the message height must be >= 0Instantiation with optional width and height: def __init__(self, width=0, height=0):Public instance method: def area(self): that returns the rectangle areaPublic instance method: def perimeter(self): that returns the rectangle perimeter:if width or height is equal to 0, perimeter is equal to 0You are not allowed to import any module

Question

Write a class Rectangle that defines a rectangle by: (based on 1-rectangle.py)Private instance attribute: width:property def width(self): to retrieve itproperty setter def width(self, value): to set it:width must be an integer, otherwise raise a TypeError exception with the message width must be an integerif width is less than 0, raise a ValueError exception with the message width must be >= 0Private instance attribute: height:property def height(self): to retrieve itproperty setter def height(self, value): to set it:height must be an integer, otherwise raise a TypeError exception with the message height must be an integerif height is less than 0, raise a ValueError exception with the message height must be >= 0Instantiation with optional width and height: def init(self, width=0, height=0):Public instance method: def area(self): that returns the rectangle areaPublic instance method: def perimeter(self): that returns the rectangle perimeter:if width or height is equal to 0, perimeter is equal to 0You are not allowed to import any module

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the Python code for the Rectangle class as per your requirements:

class Rectangle:
    def __init__(self, width=0, height=0):
        self.width = width
        self.height = height

    @property
    def width(self):
        return self.__width

    @width.setter
    def width(self, value):
        if not isinstance(value, int):
            raise TypeError("width must be an integer")
        if value < 0:
            raise ValueError("width must be >= 0")
        self.__width = value

    @property
    def height(self):
        return self.__height

    @height.setter
    def height(self, value):
        if not isinstance(value, int):
            raise TypeError("height must be an integer")
        if value < 0:
            raise ValueError("height must be >= 0")
        self.__height = value

    def area(self):
        return self.__width * self.__height

    def perimeter(self):
        if self.__width == 0 or self.__height == 0:
            return 0
        return 2 * (self.__width + self.__height)

This class has two private instance attributes: __width and __height. The @property decorator is used to make these attributes read-only, and the @width.setter and @height.setter decorators are used to validate and set the values of these attributes. The area and perimeter methods calculate and return the area and perimeter of the rectangle, respectively.

This problem has been solved

Similar Questions

Write a Rectangle class in Python language, allowing you to build a rectangle with length and width attributes.

class Rectangle: def __init__(self, width, height): self.width = width self.height = height def calculate_area(self): return self.width * self.heightWrite the definition of a class Square that:#1 inherits from the class Rectangle,#2 adds a method called calculate_perimeter that calculates and returns the square's perimeter#3 overrides the __str__ method to print out the square's area and perimeter~.5 point for the class definition~.5 for method parameters ~1 point for the Inheritance ~1 point for the new method~1 point for the overrideclass Square(Rectangle): def calculate_perimeter(self): return self.width*4 def _repr_(self): result = "Area=" + str(self.width*self.height) result += "Perimeter="+ str(self.width*4) return result

Implement a Class for a RectangleCreate a class Rectangle with attributes for length and width.Implement methods to calculate the area and perimeter of the rectangle.Ensure that the length and width are always positive.Constraints:NAExample:Sample Input:5.06.0Sample Output:Area: 30Perimeter: 22 Explanation:output is the area and perimeter of rectangle through given inputs respectively.

Question 19What is the height of the rectangle in the class Rectangle?class Rectangle(object): def __init__(self,width=2,height =3,color='r'): self.height=height self.width=width self.color=color def drawRectangle(self): import matplotlib.pyplot as plt plt.gca().add_patch(plt.Rectangle((0, 0),self.width, self.height ,fc=self.color)) plt.axis('scaled') plt.show()1 point320

Create a method display() that display the length, width, perimeter and area of an object created using an instantiation on rectangle class.

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.