In: Computer Science
please solve using jupyter notebook .
10.9- (Square Class) Write a class that implements a Square shape. The class should contain a side property. Provide an __init__ method that takes the side length as an argument. Also, provide the following read-only properties:
a) perimeter returns 4 × side.
b) area returns side × side.
c) diagonal returns the square root of the expression (2 × side2).
The perimeter, area and diagonal should not have corresponding data attributes; rather, they should use side in calculations that return the desired values. Create a Square object and display its side, perimeter, area and diagonal properties’ values.
10.9- (Square Class) Write a class that implements a Square shape. The class should contain a side property. Provide an __init__ method that takes the side length as an argument. Also, provide the following read-only properties:
a) perimeter returns 4 × side.
b) area returns side × side.
c) diagonal returns the square root of the expression (2 × side2).
The perimeter, area and diagonal should not have corresponding data attributes; rather, they should use side in calculations that return the desired values. Create a Square object and display its side, perimeter, area and diagonal properties' values
Explanation:
CODE IN PYTHON:
import math class Square: def __init__(self, side): self.side = side def perimeter(self): return 4 * self.side def area(self): return self.side * self.side def diagonal(self): return math.sqrt(2 * self.side * self.side) s = Square(4) print("Side of the square : " , s.side) print("Perimeter of the square : " , s.perimeter()) print("Area of the square : " , s.area()) print("Diagonal of the square : {0:2f} ".format(s.diagonal()))
Output:
Side of the square: 4
Perimeter of the square : 16
Area of the square : 16
Diagonal of the square : 5.656854