Answer to Question #294018 in Python for Rahul

Question #294018

Write a Python class, Rectangle. The class will have instance variables length and width


and a method which will compute the area of a rectangle. Include the constructor and


other required methods to set and get class attributes. Also, include a method isSquare(),


which returns a Boolean value indicating if the shape is a square. (Hint: use @property)



1
Expert's answer
2022-02-05T02:04:07-0500
class Rectangle:

    def __init__(self, length, width):
        """
        initializes length and width of Rectangle.
        :param length: length of rectangle
        :param width: width of rectangle
        """
        self.length = length
        self.width = width


    def set_length(self, length):
        self.length = length

    def set_width(self, width):
        self.width = width


    def get_length(self):
        return self.length

    def get_width(self):
        return self.width

    def get_area(self):
        """
        calculate and return area of Rectangle.
        using formula area = length * width
        :return: area of rectangle.
        """
        area = self.length * self.width

        return area

    def is_square(self):
        """
        check if Rectangle is Square or not
        :return: True if Rectangle is Square. otherwise False.
        """
        if self.length == self.width:
            return True
        else:
            return False

if __name__ == '__main__':
    """
    creating instance of rectangle
    and testing method of Rectangle class.
    """
    rect = Rectangle(4, 4)
    print("Rectangle methods(length=4,height=4)")
    print("get_area(): ", rect.get_area())
    print("is_square(): ", rect.is_square())

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS