Skip to content

Commit c660574

Browse files
Add files via upload
1 parent fd6d1b4 commit c660574

File tree

11 files changed

+1277
-0
lines changed

11 files changed

+1277
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Student:
2+
3+
ID = 0
4+
5+
def __init__(self, name, department, age, cgpa):
6+
self.name = name
7+
self.department = department
8+
self.age = age
9+
self.cgpa = cgpa
10+
11+
def get_details(self):
12+
print(f"ID: {Student.ID}")
13+
print(f"Name: {self.name}")
14+
print(f"Department: {self.department}")
15+
print(f"Age: {self.age}")
16+
print(f"CGPA: {self.cgpa}")
17+
18+
@classmethod
19+
def from_String(cls, str_info):
20+
name, department, age, cgpa = str_info.split("-")
21+
return cls(name, department, age, cgpa)
22+
23+
24+
s1 = Student("Samin", "CSE", 21, 3.91)
25+
s1.get_details()
26+
print("-----------------------")
27+
s2 = Student("Fahim", "ECE", 21, 3.85)
28+
s2.get_details()
29+
print("-----------------------")
30+
s3 = Student("Tahura", "EEE", 22, 3.01)
31+
s3.get_details()
32+
print("-----------------------")
33+
s4 = Student.from_String("Sumaiya-BBA-23-3.96")
34+
s4.get_details()
35+
36+
# Quesiton -1
37+
print(
38+
"Changing the class variable value will also change all the instance value that inherited the same variable from the class"
39+
)
40+
print(
41+
"But changing a instance variable will only change the varialbe value inside of that specific instance\n"
42+
)
43+
44+
# Quesiotn -2
45+
print(
46+
"Instance methods cannot be accessed without the instance of that class, but class methods don't need a instance to be used"
47+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Assassin:
2+
3+
assassin_count = 0
4+
5+
def __init__(self, name, success):
6+
self.name = name
7+
self.success = success
8+
Assassin.assassin_count += 1
9+
10+
def printDetails(self):
11+
print(f"Name: {self.name}")
12+
print(f"Success rate: {self.success}%")
13+
print(f"Total number of Assassin: {Assassin.assassin_count}")
14+
15+
@classmethod
16+
def failureRate(cls, name, rate):
17+
success = 100 - rate
18+
return cls(name, success)
19+
20+
@classmethod
21+
def failurePercentage(cls, name, percentage):
22+
success = 100 - percentage
23+
return cls(name, success)
24+
25+
26+
john_wick = Assassin("John Wick", 100)
27+
john_wick.printDetails()
28+
print("================================")
29+
nagisa = Assassin.failureRate("Nagisa", 20)
30+
nagisa.printDetails()
31+
print("================================")
32+
akabane = Assassin.failurePercentage("Akabane", 10)
33+
akabane.printDetails()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Passenger:
2+
3+
count = 0
4+
5+
def __init__(self, name):
6+
self.name = name
7+
Passenger.count += 1
8+
9+
def set_bag_weight(self, weight):
10+
self.weight = weight
11+
12+
if weight < 20:
13+
self.fare = 450
14+
15+
elif weight >= 21 and weight <= 50:
16+
self.fare = 500
17+
18+
else:
19+
self.fare = 550
20+
21+
def printDetail(self):
22+
print(f"Name: {self.name}")
23+
print(f"Bus Fare: {self.fare}")
24+
25+
26+
print("Total Passenger:", Passenger.count)
27+
p1 = Passenger("Jack")
28+
p1.set_bag_weight(90)
29+
p2 = Passenger("Carol")
30+
p2.set_bag_weight(10)
31+
p3 = Passenger("Mike")
32+
p3.set_bag_weight(25)
33+
print("=========================")
34+
p1.printDetail()
35+
print("=========================")
36+
p2.printDetail()
37+
print("=========================")
38+
p3.printDetail()
39+
print("=========================")
40+
print("Total Passenger:", Passenger.count)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Travel:
2+
3+
count = 0
4+
5+
def __init__(self, source, destination, time=1):
6+
self.source = source
7+
self.destination = destination
8+
self.time = time
9+
Travel.count += 1
10+
11+
def set_source(self, source):
12+
self.source = source
13+
14+
def set_destination(self, destination):
15+
self.destination = destination
16+
17+
def set_time(self, time):
18+
self.time = time
19+
20+
def display_travel_info(self):
21+
info_str = f"Source: {self.source}\nDestination: {self.destination}\nFlight Time: {self.time}:00"
22+
return info_str
23+
24+
25+
print("No. of Traveller =", Travel.count)
26+
print("=======================")
27+
t1 = Travel("Dhaka", "India")
28+
print(t1.display_travel_info())
29+
print("=======================")
30+
t2 = Travel("Kuala Lampur", "Dhaka")
31+
t2.set_time(23)
32+
print(t2.display_travel_info())
33+
print("=======================")
34+
t3 = Travel("Dhaka", "New_Zealand")
35+
t3.set_time(15)
36+
t3.set_destination("Germany")
37+
print(t3.display_travel_info())
38+
print("=======================")
39+
t4 = Travel("Dhaka", "India")
40+
t4.set_time(9)
41+
t4.set_source("Malaysia")
42+
t4.set_destination("Canada")
43+
print(t4.display_travel_info())
44+
print("=======================")
45+
print("No. of Traveller =", Travel.count)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Employee:
2+
def __init__(self, name, workingPeriod):
3+
self.name = name
4+
self.workingPeriod = workingPeriod
5+
6+
@classmethod
7+
def employeeByJoiningYear(cls, name, year):
8+
workingPeriod = 2021 - year
9+
return cls(name, workingPeriod)
10+
11+
@staticmethod
12+
def experienceCheck(workingPeriod, gender):
13+
if workingPeriod < 3:
14+
if gender == "male":
15+
return "He is not experienced"
16+
else:
17+
return "She is not experienced"
18+
else:
19+
if gender == "male":
20+
return "He is experienced"
21+
else:
22+
return "She is experienced"
23+
24+
25+
employee1 = Employee("Dororo", 3)
26+
employee2 = Employee.employeeByJoiningYear("Harry", 2016)
27+
print(employee1.workingPeriod)
28+
print(employee2.workingPeriod)
29+
print(employee1.name)
30+
print(employee2.name)
31+
print(Employee.experienceCheck(2, "male"))
32+
print(Employee.experienceCheck(3, "female"))
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Laptop:
2+
3+
laptopCount = 0
4+
5+
def __init__(self, name, count):
6+
self.name = name
7+
self.count = count
8+
Laptop.laptopCount += self.count
9+
10+
@classmethod
11+
def advantage(cls):
12+
print("Laptops are portable")
13+
14+
@classmethod
15+
def resetCount(cls):
16+
Laptop.laptopCount = 0
17+
18+
19+
lenovo = Laptop("Lenovo", 5)
20+
dell = Laptop("Dell", 7)
21+
print(lenovo.name, lenovo.count)
22+
print(dell.name, dell.count)
23+
print("Total number of Laptops", Laptop.laptopCount)
24+
Laptop.advantage()
25+
Laptop.resetCount()
26+
print("Total number of Laptops", Laptop.laptopCount)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Cat:
2+
3+
Number_of_cats = 0
4+
5+
def __init__(self, color, acitvity):
6+
self.color = color
7+
self.activity = acitvity
8+
9+
Cat.Number_of_cats += 1
10+
11+
def printCat(self):
12+
print(f"{self.color} cat is {self.activity}")
13+
14+
def changeColor(self, color):
15+
self.color = color
16+
17+
@classmethod
18+
def no_parameter(cls):
19+
color = "White"
20+
activity = "sitting"
21+
return cls(color, activity)
22+
23+
@classmethod
24+
def first_parameter(cls, color):
25+
activity = "sitting"
26+
return cls(color, activity)
27+
28+
@classmethod
29+
def second_parameter(cls, activity):
30+
color = "Grey"
31+
return cls(color, activity)
32+
33+
34+
print("Total number of cats:", Cat.Number_of_cats)
35+
c1 = Cat.no_parameter()
36+
c2 = Cat.first_parameter("Black")
37+
c3 = Cat("Brown", "jumping")
38+
c4 = Cat("Red", "purring")
39+
c5 = Cat.second_parameter("playing")
40+
print("=======================")
41+
c1.printCat()
42+
c2.printCat()
43+
c3.printCat()
44+
c4.printCat()
45+
c5.printCat()
46+
c1.changeColor("Blue")
47+
c3.changeColor("Purple")
48+
c1.printCat()
49+
c3.printCat()
50+
print("=======================")
51+
print("Total number of cats:", Cat.Number_of_cats)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from math import pi
2+
3+
4+
class Cylinder:
5+
6+
radius = 5
7+
height = 18
8+
9+
def __init__(self, radius, height):
10+
11+
print(f"Default radius = {Cylinder.radius} and height = {Cylinder.height}.")
12+
13+
Cylinder.radius = radius
14+
Cylinder.height = height
15+
16+
print(f"Updated: radius = {Cylinder.radius} and height = {Cylinder.height}.")
17+
18+
@classmethod
19+
def swap(cls, height, radius):
20+
return cls(radius, height)
21+
22+
@classmethod
23+
def changeFormat(cls, info_str):
24+
radius, height = info_str.split("-")
25+
radius = float(radius)
26+
height = float(height)
27+
return cls(radius, height)
28+
29+
@staticmethod
30+
def area(radius, height):
31+
area = 2 * pi * (radius ** 2) + 2 * pi * radius * height
32+
print(f"Area: {area}")
33+
34+
@staticmethod
35+
def volume(radius, height):
36+
volume = pi * (radius ** 2) * height
37+
print(f"Volume: {volume}")
38+
39+
40+
c1 = Cylinder(0, 0)
41+
Cylinder.area(c1.radius, c1.height)
42+
Cylinder.volume(c1.radius, c1.height)
43+
print("===============================")
44+
c2 = Cylinder.swap(8, 3)
45+
c2.area(c2.radius, c2.height)
46+
c2.volume(c2.radius, c2.height)
47+
print("===============================")
48+
c3 = Cylinder.changeFormat("7-13")
49+
c3.area(c3.radius, c3.height)
50+
c3.volume(c3.radius, c3.height)
51+
print("===============================")
52+
Cylinder(0.3, 5.56).area(Cylinder.radius, Cylinder.height)
53+
print("===============================")
54+
Cylinder(3, 5).volume(Cylinder.radius, Cylinder.height)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class Student:
2+
3+
total_student = 0
4+
brac_student = 0
5+
other_student = 0
6+
7+
def __init__(self, name, dept, institute="BRAC University"):
8+
self.name = name
9+
self.dept = dept
10+
self.institute = institute
11+
12+
if institute == "BRAC University":
13+
Student.brac_student += 1
14+
else:
15+
Student.other_student += 1
16+
17+
Student.total_student += 1
18+
19+
def individualDetail(self):
20+
print(f"Name: {self.name}")
21+
print(f"Department: {self.dept}")
22+
print(f"Institution: {self.institute}")
23+
24+
@classmethod
25+
def printDetails(cls):
26+
print(f"Total Student(s): {cls.total_student}")
27+
print(f"BRAC University Student(s): {cls.brac_student}")
28+
print(f"Other Institution Student(s): {cls.other_student}")
29+
30+
@classmethod
31+
def createStudent(cls, name, dept, institute=None):
32+
if institute == None:
33+
return cls(name, dept)
34+
else:
35+
return cls(name, dept, institute)
36+
37+
38+
Student.printDetails()
39+
print("#########################")
40+
mikasa = Student("Mikasa Ackerman", "CSE")
41+
mikasa.individualDetail()
42+
print("------------------------------------------")
43+
Student.printDetails()
44+
print("========================")
45+
harry = Student.createStudent(
46+
"Harry Potter", "Defence Against Dark Arts", "Hogwarts School"
47+
)
48+
harry.individualDetail()
49+
print("-------------------------------------------")
50+
Student.printDetails()
51+
print("=========================")
52+
levi = Student.createStudent("Levi Ackerman", "CSE")
53+
levi.individualDetail()
54+
print("--------------------------------------------")
55+
Student.printDetails()

0 commit comments

Comments
 (0)