Untitled
Anonymous
Plain Text
02/07/2025 2:38 AM
1.2 KB
18
Indexable
Never
#(a) Create a class Square that has:
class Square:
#Attributes:
#sides – a class variable to store the number of sides
sides = 4
#Methods:
# __init__ – initialize the __side_length attributes
def __init__(self, length):
self.__side_length = length #an instance var to store the length of side
# side – return the length of side (getter)
def side(self):
return self.__side_length
# area – return the area of the square instance
def area(self):
return self.__side_length ** 2
# perimeter – return the perimeter of the square instance
def perimeter(self):
return self.__side_length * self.sides
# diagonal – return the diagonal of the square instance
def diagonal(self):
return (self.__side_length ** 2 * 2) ** 0.5 # or math.sqrt(xxxx)
#(b) Complete the following client program:
if __name__ == "__main__":
#(i) Create a square instance square1 with a side of 5.
Square1 = Square(5)
#(ii) Create another square instance square2 with a side of 10.
#(iii) Use the methods created in part (a) to print the details of the squares following the sample output below
Editor is loading...
Leave a Comment