-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog52aclass.py
35 lines (28 loc) · 1007 Bytes
/
prog52aclass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Shape:
# Constructor: sets up class data
def __init__(self, length, width):
self.length = length
self.width = width
self._area = 0 # _ prefix basically means 'private' so
self._perim = 0 # it should only be called in the class
# Mutator/Setter Method(s): modifies class data
def calculate(self):
self._area = self.length * self.width
self._perim = 2 * self.length + 2 * self.width
# Accessor/Getter Method(s): returns class data
def get_area(self):
return self._area
def get_perimeter(self):
return self._perim
def main():
length = int(input("Enter length: "))
width = int(input("Enter width: "))
# Make a new 'Shape' object/instance
shape = Shape(length, width) # Call 'Shape' constructor/__init__ method
# shape.length = 5
shape.calculate()
print("Area:", shape.get_area())
print("Perimeter:", shape.get_perimeter())
pass
if __name__ == "__main__":
main()