Given the following code: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
Question
Given the following code: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
Solution
To define a class Square that inherits from the class Rectangle and includes the required methods, follow these steps:
Step 1: Define the class Square and specify that it inherits from the class Rectangle.
class Square(Rectangle):
Step 2: Add a method called calculate_perimeter that calculates and returns the square's perimeter.
def calculate_perimeter(self):
return 4 * self.width
Step 3: Override the str method to print out the square's area and perimeter.
def __str__(self):
area = self.calculate_area()
perimeter = self.calculate_perimeter()
return f"Square - Area: {area}, Perimeter: {perimeter}"
The complete definition of the class Square would be as follows:
class Square(Rectangle):
def calculate_perimeter(self):
return 4 * self.width
def __str__(self):
area = self.calculate_area()
perimeter = self.calculate_perimeter()
return f"Square - Area: {area}, Perimeter: {perimeter}"
This class Square inherits the width and height attributes from the Rectangle class and adds the calculate_perimeter method to calculate the square's perimeter. The str method is overridden to print out the square's area and perimeter when the object is converted to a string.
Similar Questions
What is the output of the below code?Code:class Square: def __init__(self, side): self.side = side self.area = side*side s1 = Square(Square(Square(2).side).area)print(s1.area)
Write a class Rectangle that defines a rectangle by: (based on 5-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 >= 0Public class attribute number_of_instances:Initialized to 0Incremented during each new instance instantiationDecremented during each instance deletionInstantiation 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 has to be equal to 0print() and str() should print the rectangle with the character #:if width or height is equal to 0, return an empty stringrepr() should return a string representation of the rectangle to be able to recreate a new instance by using eval()Print the message Bye rectangle... (... being 3 dots not ellipsis) when an instance of Rectangle is deletedYou are not allowed to import any module
What will be the output of the following code? python class Shape: def area(self): pass class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * self.radius * self.radius class Square(Shape): def __init__(self, side): self.side = side def area(self): return self.side * self.side shapes = [Circle(2), Square(3)] for shape in shapes: print(shape.area())3.14\n33.14\n912.56\n912.56\n3
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
Predict the output of the following code snippetclass Area1: def __init__(self,feet): self.feet=feetclass Area2: def __init__(self,feet): self.feet=feeta1=Area1(12)a2=Area2(12)print("Total Area: ",a1+a2)24Error in passing paramter as a2=Area2(12) Error in print("Total Area: ",a1+a2)Error in self.feet=feet in two classes
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.