forked from fenyx-it-academy/Class4-PythonModule-Week6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate a subclass of Shape.py
More file actions
123 lines (96 loc) · 3.78 KB
/
Create a subclass of Shape.py
File metadata and controls
123 lines (96 loc) · 3.78 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
Create a class named Triangle and Rectangle.
Create a subclass named Square inherited from Rectangle.
Create a subclass named Cube inherited from Square.
Create a subclass named Pyramid multiple inherited both from Triangle and Square.
Two dimensional classes (Triangle, Rectangle and Square) should have:
* its dimensions as attributes.(can be inherited from a superclass)
* methods which calculate its area and perimeter separately.
Three dimensional classes (Cube and Pyramid) should have:
* its dimensions as attributes which are inherited from a superclass
* its extra dimensions if there is. (hint: maybe for Pyramid)
methods which calculate its volume and surface area separately. (surface area is optional, you may not do this)
"""
'''
class Shape():
def _init_(self,a,b,c):
self.a = float(a)
self.b = float(b)
self.c = float(c)
a= input("a=")
b= input("b=")
c= input("c=")'''
class Triangle():
def __init__(self, a=0, b=0, c=0):
self.a = float(input("ucgenin a kenar uzunlugunu giriniz="))
self.b = float(input("ucgenin b kenar uzunlugunu giriniz="))
self.c = float(input("ucgenin c kenar uzunlugunu giriniz="))
def get_area(self):
# ucgen alanini hesaplar
s = (self.a + self.b + self.c) / 2
return (s*(s-self.a)*(s-self.b)*(s-self.c)) ** 0.5
def get_perimeter(self):
# ucgen cevresini hesaplar
return self.a + self.b + self.c
def __str__(self):
print( "ucgenin alani : {} m2 \n"
"ucgenin cevresi : {} m"
.format(self.get_area(), self.get_perimeter()))
class Rectangle():
def __init__(self, a=0, b=0):
self.a = float(input("dikdortgenin a kenar uzunlugunu giriniz="))
self.b = float(input("dikdortgenin b kenar uzunlugunu giriniz="))
def get_area(self):
# dikdortgenin alanini hesaplar
return self.a*self.b
def get_perimeter(self):
# dikdortgenin cevresini hesaplar
return (self.a + self.b)*2
def __str__(self):
print( "dikdortgenin alani : {} m2 \n"
"dikdortgenin cevresi : {} m"
.format(self.get_area(), self.get_perimeter()))
class Square(Rectangle):
def __init__(self,a=0):
self.a = float(input("karenin kenar uzunlugunu giriniz="))
self.b = self.a
def __str__(self):
print("karenin alani : {} m2 \n"
"karenin cevresi : {} m"
.format(self.get_area(), self.get_perimeter()))
class Cube(Square):
def __init__(self,a=0):
self.a = float(input("kupun kenar uzunlugunu giriniz="))
self.b = self.a
def get_volume(self):
# kupun hacmini hesaplar
return self.a**3
def __str__(self):
print("kupun hacmi : {} m3 \n"
"kupun alani : {} m2 \n"
"kupun cevresi : {} m"
.format(self.get_volume(), 6*self.get_area(), 3*self.get_perimeter()))
class Pyramid (Square,Triangle):
def __init__(self,a=0,pyramid_height=0):
self.a = float(input("Kere Piramidin kenar uzunlugunu giriniz="))
self.pyramid_height = float(input("Kare Piramidin yuksekligini giriniz="))
self.b = self.a
self.c = self.a
def get_volume(self):
# Piramidin hacmini hesaplar
return self.a**2*self.pyramid_height/3
def __str__(self):
print("Piramidin hacmi : {} m3 \n"
"Piramidin alani : {} m2 \n"
"Piramidin cevresi : {} m"
.format(self.get_volume(), self.get_area(), self.get_perimeter()))
#t = Triangle()
#t.__str__()
#d = Rectangle()
#d.__str__()
#k = Square()
#k.__str__()
#kk = Cube()
#kk.__str__()
p = Pyramid()
p.__str__()