-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercises 10.py
More file actions
198 lines (167 loc) · 6.64 KB
/
Copy pathExercises 10.py
File metadata and controls
198 lines (167 loc) · 6.64 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
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
"""""
1. Write an Elevator class that receives the numbers of the bottom and top floors as initializer parameters.
The elevator has methods go_to_floor, floor_up and floor_down. A new elevator is always at the bottom floor.
If you make elevator h for example the method call h.go_to_floor(5), the method calls either the floor_up or
floor_down methods as many times as it needs to get to the fifth floor. The methods run the elevator one floor up or
down and tell what floor the elevator is after each move. Test the class by creating an elevator in the main program,
tell it to move to a floor of your choice and then back to the bottom floor.
"""""
class Elevator:
def __init__(self):
self.floor = 0
def floor_up(self):
self.floor += 1
def floor_down(self):
self.floor -= 1
def go_to_floor(self,num):
while True:
if self.floor > num:
self.floor_down()
print(self.floor)
elif self.floor < num:
print(self.floor)
self.floor_up()
else:
break
h = Elevator()
h.go_to_floor(5)
h.go_to_floor(2)
"""""
2. Extend the previous program by creating a Building class. The initializer parameters for the class are the numbers of
the bottom and top floors and the number of elevators in the building. When a building is created, the building creates
the required number of elevators. The list of elevators is stored as a property of the building. Write a method called
run_elevator that accepts the number of the elevator and the destination floor as its parameters. In the main program,
write the statements for creating a new building and running the elevators of the building.
"""
class Elevator:
def __init__(self):
self.floor = 0
def floor_up(self):
self.floor += 1
def floor_down(self):
self.floor -= 1
def go_to_floor(self,num):
while True:
if self.floor > num:
self.floor_down()
print(self.floor)
elif self.floor < num:
print(self.floor)
self.floor_up()
else:
break
class Building:
def __init__(self, num):
self.elevators = []
for i in range(num):
self.elevators.append(Elevator())
def run_elevator(self, ele_num, floor):
self.elevators[ele_num].go_to_floor(floor)
b = Building(5)
b.run_elevator(3, 6)
print(b.elevators[3].floor)
"""""
3. Extend the program again by adding a method fire_alarm that does not receive any parameters and
moves all elevators to the bottom floor. Continue the main program by causing a fire alarm in your building.
"""
class Elevator:
def __init__(self):
self.floor = 0
def floor_up(self):
self.floor += 1
def floor_down(self):
self.floor -= 1
def go_to_floor(self,num):
while True:
if self.floor > num:
self.floor_down()
print(self.floor)
elif self.floor < num:
print(self.floor)
self.floor_up()
else:
break
class Building:
def __init__(self, num):
self.elevators = []
for i in range(num):
self.elevators.append(Elevator())
def run_elevator(self, ele_num, floor):
self.elevators[ele_num].go_to_floor(floor)
def fire_alarm(self):
for i in self.elevators:
i.go_to_floor(0)
b = Building(5)
b.run_elevator(3, 6)
print(b.elevators[3].floor)
b.fire_alarm()
"""""
4. This exercise continues the previous car race exercise from the last exercise set. Write a Race class that has the following properties:
name, distance in kilometers and a list of cars participating in the race. The class has an initializer that receives the name, kilometers,
and car list as parameters and sets their values to the corresponding properties in the class. The class has the following methods:
hour_passes, which performs the operations done once per hour in the original exercise: generates a random change of speed for each car and calls their drive method.
print_status, which prints out the current information of each car as a clear, formatted table.
race_finished, which returns True if any of the cars has reached the finish line, meaning that they have driven the entire distance of the race.
Write a main program that creates an 8000-kilometer race called Grand Demolition Derby. The new race is given a list of
ten cars similarly to the earlier exercise. The main program simulates the progressing of the race by calling the hour_passes in a loop,
after which it uses the race_finished method to check if the race has finished. The current status is printed out using the print_status
method every ten hours and then once more at the end of the race.
"""
import random
class Car:
def __init__(self, registration, maxspeed):
self.registration = registration
self.maxspeed = maxspeed
self.currentspeed = 0
self.distance = 0
def accerelate(self, addspeed):
accelaretion = self.currentspeed + addspeed
if self.maxspeed > accelaretion:
self.currentspeed+=addspeed
if accelaretion < 0:
self.currentspeed = 0
def drive(self, hours):
self.distance = self.distance + self.currentspeed*hours
def properties(car_list):
for i in car_list:
print(i.registration, i.maxspeed, i.currentspeed, i.distance)
def distance(car_list):
for i in car_list:
if i.distance > 10000:
return True
def update_accerelate(car_list):
for i in car_list:
i.accerelate(random.randint(-10, 15))
i.drive(1)
class Race:
def __init__(self, name, d_km, car_list):
self.name = name
self.d_km = d_km
self.car_list = car_list
def hour_passes(self):
update_accerelate(self.car_list)
for i in self.car_list:
i.drive(1)
def race_finished(self):
for i in self.car_list:
if i.distance < self.d_km:
return False
def __repr__(self):
text = "Speed (km/h) | Distance (km)`\n"
for i in self.car_list:
text+= str(i.currentspeed)+ "km/h | "+ str(i.distance)+ "km\n"
return text
def print_status(self):
for i in car_list:
i.drive(10)
print(i.registration, i.maxspeed, i.currentspeed, i.distance)
car_list = []
for i in range(10):
car_list.append(Car("ABC-" + str(i+1), random.randint(100,200)))
r = Race("Grand Demolition Derby", 8000,car_list)
for i in range(8000):
print(i)
if r.race_finished():
break
r.hour_passes()
r.print_status()