This repository was archived by the owner on Nov 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.py
More file actions
138 lines (103 loc) · 3.37 KB
/
class.py
File metadata and controls
138 lines (103 loc) · 3.37 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
124
125
126
127
128
129
130
131
132
133
134
135
# define elephant size
elephant_x = 5
elephant_y = 5
elephamt_h = 5
# define refrigerator size
refrigerator_x = 6
refrigerator_y = 6
refrigerator_h = 6
def open_refrigerator_door():
print('Refrigerator door is opened')
def package_elephant():
print('Elephant is packaged')
def put_elephant_to_refrigerator():
print('Elephant is in the fridge already')
def close_refrigerator_door():
print('Close the refrigerator door')
# compare size
if elephant_x < refrigerator_x and elephant_y < refrigerator_y and elephamt_h < refrigerator_h:
open_refrigerator_door()
package_elephant()
put_elephant_to_refrigerator()
close_refrigerator_door()
else:
print('refrigerator is too small to put elephant')
##################
class Elephant:
def __init__(self, x, y, h):
self.x = x
self.y = y
self.h = h
def package(self):
print('Elephant is packaged')
class Refrigerator:
def __init__(self, x, y, h):
self.x = x
self.y = y
self.h = h
self.is_door_open = False
def open_door(self):
self.is_door_open = True
print('Refrigerator door is opened')
def close_door(self):
self.is_door_open = False
print('Close the refrigerator door')
def put_elephant(self, elephant):
if not self.is_door_open:
self.open_door()
if elephant.x < self.x and elephant.y < self.y and elephant.h < self.h:
elephant.package()
print('Elephant is in the fridge already')
else:
print('Refrigerator is too small to put elephant')
self.close_door()
# Define element sizes
elephant_x = 5
elephant_y = 5
elephant_h = 5
# Define refrigerator sizes
refrigerator_x = 6
refrigerator_y = 6
refrigerator_h = 6
# Create instances of Elephant and Refrigerator classes
elephant = Elephant(elephant_x, elephant_y, elephant_h)
refrigerator = Refrigerator(refrigerator_x, refrigerator_y, refrigerator_h)
# Put the elephant in the refrigerator
refrigerator.put_elephant(elephant)
"""
1. The Elephant class contains the package method.
2. The Refrigerator class contains methods for opening and closing the door (open_door and close_door)
as well as putting the elephant inside (put_elephant).
3. The logic to determine if the elephant fits into the refrigerator is now part of the put_elephant method.
4. We create instances of the Elephant and Refrigerator classes and use the put_elephant method to put the elephant into the refrigerator.
"""
class Car:
def __init__(self, make, model, year, color, mileage, num):
self.make = make
self.model = model
self.year = year
self.color = color
self.mileage = mileage
self.num = num
def drive(self, distance):
self.mileage += distance
def paint(self, new_color):
self.color = new_color
def re_register(self, new_num):
self.num = new_num
def display_info(self):
print(f"Make: {self.make}")
print(f"Model: {self.model}")
print(f"Year: {self.year}")
print(f"Color: {self.color}")
print(f"Mileage: {self.mileage}")
print(f"plate number: {self.num}")
# Test the Car class
car1 = Car("Toyota", "Camry", 2020, "Blue", 15000, "DUR 888")
car1.display_info()
car1.drive(100)
car1.paint("Red")
car1.display_info()
################
car1.re_register('DUR 666')
car1.display_info()