-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoop2.py
64 lines (55 loc) · 1.62 KB
/
oop2.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
"""OOP2.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/
"""
#OOP_part2 HW
import math
## __str__ overwriter
class Shape:
def __init__(self,x,y):
self.__x = x
self.__y = y
def getXYLoc(self):
return (self.__x, self.__y)
def setXYLoc(self,x, y):
self.__x = x
self.__y = y
def calcArea(self):
raise NotImplementedError("Method calcArea not implemented")
def calcPerimeter(self):
raise NotImplementedError("Method calcPerimeter not implemented")
class Circle(Shape):
def __init__(self,x,y,r):
Shape.__init__(self,x,y)
self.__radius = r
def calcArea(self):
return math.pi * self.__radius ** 2
def calcPerimeter(self):
return 2 * math.pi * self.__radius # perimeter of circle = 2*pi*r
class Square(Shape):
def __init__(self,x,y,s):
Shape.__init__(self,x, y)
self.__size = s
def calcArea(self):
return self.__size ** 2
def calcPerimeter(self):
return self.__size * 4 # perimeter of square is 4 * one side's length
class Triangle(Shape):
def __init__(self,x,y,s):
Shape.__init__(self, x, y)
self.__side = s
def calcArea(self):
return (self.__side ** 2) * math.sqrt(3)/4.0
def calcPerimeter(self):
return self.__side * 3 # perimeter of triangle is 3 * one side's length
shapes_list = (Circle(0,0,1),Circle(0,1,3),Square(0,0,4), Triangle(0,0,5))
total_area = 0
for shape in shapes_list:
total_area += shape.calcArea()
print(total_area)
total_perimeter = 0
for shape in shapes_list:
total_perimeter += shape.calcPerimeter()
print(total_perimeter)