-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAssignment 6 solve.py
469 lines (386 loc) · 11.2 KB
/
Assignment 6 solve.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# Task 1
class Student:
ID = 0
def __init__(self, name, department, age, cgpa):
self.name = name
self.department = department
self.age = age
self.cgpa = cgpa
def showDetails(self):
Student.ID += 1
print("ID:",Student.ID)
print("Name:",self.name)
print("Department:",self.department)
print("Age:",self.age)
print("CGPA:",self.cgpa)
@classmethod
def from_String(cls, all):
name,department,age,cgpa = all.split("-")
return Student(name, department, age, cgpa)
s1 = Student("Samin", "CSE", 21, 3.91)
s1.showDetails()
print("-----------------------")
s2 = Student("Fahim", "ECE", 21, 3.85)
s2.showDetails()
print("-----------------------")
s3 = Student("Tahura", "EEE", 22, 3.01)
s3.showDetails()
print("-----------------------")
s4 = Student.from_String("Sumaiya-BBA-23-3.96")
s4.showDetails()
# Quesiton -1
print("Changing the class variable value will also change all the instance value that inherited the same variable from the class")
print("But changing a instance variable will only change the varialbe value inside of that specific instance\n")
# Quesiotn -2
print("Instance methods cannot be accessed without the instance of that class, but class methods don't need a instance to be used")
# Task 2
class Assassin:
total = 0
def __init__(self, name, per):
self.name = name
self.per = per
def printDetails(self):
Assassin.total += 1
print("Name:",self.name)
print("Success rate:",self.per,"%")
print("Total number of Assassin:",Assassin.total)
@classmethod
def failureRate(cls, name, per):
s_per = 100 - per
return Assassin(name, s_per)
@classmethod
def failurePercentage(cls, name, str):
s_per = 100 - int(str[:len(str)-1])
return Assassin(name, s_per)
john_wick = Assassin('John Wick', 100)
john_wick.printDetails()
print('================================')
nagisa = Assassin.failureRate("Nagisa", 20)
nagisa.printDetails()
print('================================')
akabane = Assassin.failurePercentage("Akabane", "10%")
akabane.printDetails()
# Task 3
class Passenger:
count = 0
def __init__(self, name):
self.name = name
self.fair = 450
def set_bag_weight(self,weight):
if weight <= 20:
self.fair += 0
elif 21 <= weight <= 50:
self.fair += 50
else:
self.fair += 100
def printDetail(self):
Passenger.count += 1
print("Name:",self.name)
print("Bus Fare:",self.fair,"taka")
print("Total Passenger:", Passenger.count)
p1 = Passenger("Jack")
p1.set_bag_weight(90)
p2 = Passenger("Carol")
p2.set_bag_weight(10)
p3 = Passenger("Mike")
p3.set_bag_weight(25)
print("=========================")
p1.printDetail()
print("=========================")
p2.printDetail()
print("=========================")
p3.printDetail()
print("=========================")
print("Total Passenger:", Passenger.count)
# Task 4
class Travel:
count = 0
def __init__(self, source, destination):
self.source = source
self.destination = destination
self.time = "1:00"
def display_travel_info(self):
Travel.count += 1
return f"Source:{self.source}\nDestination:{self.destination}\nFlight Time:{self.time}"
def set_time(self, t):
t = str(t)
self.time = t + ":00"
def set_destination(self, d):
self.destination = d
def set_source(self, s):
self.source = s
print("No. of Traveller =", Travel.count)
print("=======================")
t1 = Travel("Dhaka","India")
print(t1.display_travel_info())
print("=======================")
t2 = Travel("Kuala Lampur","Dhaka")
t2.set_time(23)
print(t2.display_travel_info())
print("=======================")
t3 = Travel("Dhaka","New_Zealand")
t3.set_time(15)
t3.set_destination("Germany")
print(t3.display_travel_info())
print("=======================")
t4 = Travel("Dhaka","India")
t4.set_time(9)
t4.set_source("Malaysia")
t4.set_destination("Canada")
print(t4.display_travel_info())
print("=======================")
print("No. of Traveller =", Travel.count)
# Task 5
class Employee:
def __init__(self, name, workingPeriod):
self.name = name
self.workingPeriod = workingPeriod
@classmethod
def employeeByJoiningYear(cls, name, year):
exp = 2022 - year
return Employee(name, exp)
@staticmethod
def experienceCheck(wp, g):
if g == "male":
if wp < 3:
return "He is not experienced"
else:
return "He is experienced"
elif g == "female":
if wp < 3:
return "She is not experienced"
else:
return "She is experienced"
employee1 = Employee('Dororo', 3)
employee2 = Employee.employeeByJoiningYear('Harry', 2016)
print(employee1.workingPeriod)
print(employee2.workingPeriod)
print(employee1.name)
print(employee2.name)
print(Employee.experienceCheck(2, "male"))
print(Employee.experienceCheck(3, "female"))
# Task 6
class Laptop:
laptopCount = 0
def __init__(self, name, count):
Laptop.laptopCount += count
self.name = name
self.count = count
@classmethod
def resetCount(cls):
cls.laptopCount = 0
@classmethod
def advantage(cls):
print("Laptops are portable")
lenovo = Laptop("Lenovo", 5);
dell = Laptop("Dell", 7);
print(lenovo.name, lenovo.count)
print(dell.name, dell.count)
print("Total number of Laptops", Laptop.laptopCount)
Laptop.advantage()
Laptop.resetCount()
print("Total number of Laptops", Laptop.laptopCount)
# Task 7
class Cat:
Number_of_cats = 0
def __init__(self, name, work):
Cat.Number_of_cats += 1
self.name = name
self.work = work
def changeColor(self, name):
self.name = name
def printCat(self):
print(f"{self.name} cat is {self.work}")
@classmethod
def no_parameter(cls):
name = "White"
work = "Sitting"
return Cat(name,work)
@classmethod
def first_parameter(cls, name):
work = "sitting"
return Cat(name,work)
@classmethod
def second_parameter(cls, work):
name = "Grey"
return Cat(name, work)
print("Total number of cats:", Cat.Number_of_cats)
c1 = Cat.no_parameter()
c2 = Cat.first_parameter("Black")
c3 = Cat("Brown", "jumping")
c4 = Cat("Red", "purring")
c5 = Cat.second_parameter("playing")
print("=======================")
c1.printCat()
c2.printCat()
c3.printCat()
c4.printCat()
c5.printCat()
c1.changeColor("Blue")
c3.changeColor("Purple")
c1.printCat()
c3.printCat()
print("=======================")
print("Total number of cats:", Cat.Number_of_cats)
# Task 8
import math
class Cylinder:
radius = 5
height = 18
def __init__(self, radius, height):
print("Default radius=", Cylinder.radius, "and height=", Cylinder.height)
Cylinder.radius = radius
Cylinder.height = height
print("Updated radius=", Cylinder.radius, "and height=", Cylinder.height)
@classmethod
def swap(cls, height, radius):
return cls(radius, height)
@classmethod
def changeFormat(cls, st):
lst = st.split("-")
radius = float(lst[0])
height = float(lst[1])
return cls(radius, height)
@staticmethod
def area(radius, height):
ar = 2 * math.pi * radius * radius + 2 * math.pi * radius * height
print("Area:", ar)
@staticmethod
def volume(radius, height):
vol = math.pi * radius * radius * height
print("Volume:", vol)
c1 = Cylinder(0,0)
Cylinder.area(c1.radius,c1.height)
Cylinder.volume(c1.radius,c1.height)
print("===============================")
c2 = Cylinder.swap(8,3)
c2.area(c2.radius,c2.height)
c2.volume(c2.radius,c2.height)
print("===============================")
c3 = Cylinder.changeFormat("7-13")
c3.area(c3.radius,c3.height)
c3.volume(c3.radius,c3.height)
print("===============================")
Cylinder(0.3,5.56).area(Cylinder.radius,Cylinder.height)
print("===============================")
Cylinder(3,5).volume(Cylinder.radius,Cylinder.height)
# Task 9
class Student:
t_s = 0
b_s = 0
o_s = 0
@classmethod
def printDetails(cls):
print("Total Student(s):",cls.t_s)
print("BRAC University Student(s):",cls.b_s)
print("Other Institution Student(s):",cls.o_s)
def __init__(self, name, dept, inst = "BRAC University"):
self.name = name
self.dept = dept
self.inst = inst
Student.t_s += 1
if self.inst == "BRAC University":
Student.b_s += 1
else:
Student.o_s += 1
def individualDetail(self):
print("Name:",self.name)
print("Department:",self.dept)
print("Institution:",self.inst)
@classmethod
def createStudent(cls, name, dept, inst = "BRAC University"):
cls.name = name
cls.dept = dept
cls.inst = inst
return Student(cls.name,cls.dept,cls.inst)
Student.printDetails()
print('#########################')
mikasa = Student('Mikasa Ackerman', "CSE")
mikasa.individualDetail()
print('------------------------------------------')
Student.printDetails()
print('========================')
harry = Student.createStudent('Harry Potter', "Defence Against Dark Arts", "Hogwarts School")
harry.individualDetail()
print('-------------------------------------------')
Student.printDetails()
print('=========================')
levi = Student.createStudent("Levi Ackerman", "CSE")
levi.individualDetail()
print('--------------------------------------------')
Student.printDetails()
# Task 10
class SultansDine:
total_branch = 0
total_sell = 0
branch_info = []
def __init__(self, branch):
self.name = branch
SultansDine.total_branch += 1
def sellQuantity(self, quantity):
if quantity < 10:
self.branch_sell = quantity * 300
elif quantity < 20:
self.branch_sell = quantity * 350
else:
self.branch_sell = quantity * 400
SultansDine.total_sell += self.branch_sell
def branchInformation(self):
print(f"Branch Name: {self.name}")
print(f"Branch Sell: {self.branch_sell}")
SultansDine.branch_info.append(self.name)
SultansDine.branch_info.append(self.branch_sell)
@classmethod
def details(cls):
print(f"Total Number of branch(s): {cls.total_branch}")
print(f"Total Sell: {cls.total_sell}")
for index in range(0, len(SultansDine.branch_info), 2):
print(f"Branch Name: {SultansDine.branch_info[index]}, Branch Sell: {SultansDine.branch_info[index + 1]} Taka")
print(f"Branch consists of total sell's: {(SultansDine.branch_info[index + 1] / SultansDine.total_sell) * 100:.2f}%")
SultansDine.details()
print('########################')
dhanmodi = SultansDine('Dhanmondi')
dhanmodi.sellQuantity(25)
dhanmodi.branchInformation()
print('-----------------------------------------')
SultansDine.details()
print('========================')
baily_road = SultansDine('Baily Road')
baily_road.sellQuantity(15)
baily_road.branchInformation()
print('-----------------------------------------')
SultansDine.details()
print('========================')
gulshan = SultansDine('Gulshan')
gulshan.sellQuantity(9)
gulshan.branchInformation()
print('-----------------------------------------')
SultansDine.details()
# Task 11 (Tracing)
class Puzzle:
x = 0
def methodA(self):
Puzzle.x = 5
z = Puzzle.x + self.methodB(Puzzle.x)
print(Puzzle.x, z)
z = self.methodB(z + 2) + Puzzle.x
print(Puzzle.x, z)
self.methodB(Puzzle.x, z)
print(Puzzle.x, z)
def methodB(self, *args):
if len(args) == 1:
y = args[0]
Puzzle.x = y + Puzzle.x
print(Puzzle.x, y)
return Puzzle.x + 3
else:
z, x = args
z = z + 1
x = x + 1
print(z, x)
p = Puzzle()
p.methodA()
# p.methodA()
# p = Puzzle()
# p.methodA()
# p.methodB(7)