Skip to content

Commit 7f64bc4

Browse files
committed
Exercises were done already
1 parent ff6f41e commit 7f64bc4

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

lab-oop_in_python.ipynb

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,31 @@
3232
"cell_type": "code",
3333
"execution_count": null,
3434
"metadata": {},
35-
"outputs": [],
35+
"outputs": [
36+
{
37+
"data": {
38+
"text/plain": [
39+
"100"
40+
]
41+
},
42+
"execution_count": 15,
43+
"metadata": {},
44+
"output_type": "execute_result"
45+
}
46+
],
3647
"source": [
3748
"# Your code here\n",
3849
"class Vehicle:\n",
39-
" # Hint: Define __init__ method with parameters for max_speed and mileage\n",
40-
" pass\n",
50+
" color = 'red'\n",
51+
" def __init__(self, max_speed, mileage):\n",
52+
" self.max_speed = max_speed\n",
53+
" self.mileage = mileage\n",
54+
" # Hint: Define __init__ method with parameters for max_speed and mileage\n",
55+
" pass\n",
4156
"\n",
4257
"# Example instantiation\n",
43-
"modelX = Vehicle() # Create an instance of Vehicle\n",
44-
"\n",
58+
"modelX = Vehicle(max_speed= 100, mileage = 1000) # Create an instance of Vehicle\n",
59+
"modelX.max_speed\n",
4560
"# Print attributes\n",
4661
"print(modelX.max_speed, modelX.mileage) # Expected output: (value of max_speed, value of mileage)"
4762
]
@@ -56,7 +71,7 @@
5671
},
5772
{
5873
"cell_type": "code",
59-
"execution_count": 2,
74+
"execution_count": 7,
6075
"metadata": {},
6176
"outputs": [
6277
{
@@ -87,7 +102,7 @@
87102
},
88103
{
89104
"cell_type": "code",
90-
"execution_count": 3,
105+
"execution_count": 8,
91106
"metadata": {},
92107
"outputs": [
93108
{
@@ -118,28 +133,31 @@
118133
},
119134
{
120135
"cell_type": "code",
121-
"execution_count": 4,
136+
"execution_count": 26,
122137
"metadata": {},
123138
"outputs": [
124139
{
125140
"name": "stdout",
126141
"output_type": "stream",
127142
"text": [
128-
"Base fare\n"
143+
"Base Fare + 100$\n"
129144
]
130145
}
131146
],
132147
"source": [
133148
"# Your code here\n",
134149
"class Vehicle:\n",
135-
" def fare(self):\n",
150+
" def fare1(self):\n",
136151
" return \"Base fare\"\n",
137152
"\n",
138153
"class Bus(Vehicle):\n",
139-
" # Hint: Override fare method to include extra charges\n",
140-
" pass\n",
154+
" def __init__(self, maintenance):\n",
155+
" self.maintenance = maintenance\n",
141156
"\n",
142-
"school_bus = Bus()\n",
157+
" def fare(self):\n",
158+
" return f\"Base Fare + {self.maintenance}\"\n",
159+
"\n",
160+
"school_bus = Bus(maintenance = '100$')\n",
143161
"print(school_bus.fare()) # Expected output: \"Base fare with extra charge\""
144162
]
145163
},
@@ -153,7 +171,7 @@
153171
},
154172
{
155173
"cell_type": "code",
156-
"execution_count": 5,
174+
"execution_count": 10,
157175
"metadata": {},
158176
"outputs": [
159177
{
@@ -188,13 +206,14 @@
188206
},
189207
{
190208
"cell_type": "code",
191-
"execution_count": 6,
209+
"execution_count": 36,
192210
"metadata": {},
193211
"outputs": [
194212
{
195213
"name": "stdout",
196214
"output_type": "stream",
197215
"text": [
216+
"Total Bus fare is: 5500.0\n",
198217
"Total Bus fare is: 5000\n"
199218
]
200219
}
@@ -211,11 +230,17 @@
211230
" return self.capacity * 100\n",
212231
"\n",
213232
"class Bus(Vehicle):\n",
214-
" # Hint: Override fare method to include maintenance charge\n",
215-
" pass\n",
233+
" def __init__(self, name, mileage, capacity, maintenance):\n",
234+
" super().__init__( name, mileage, capacity)\n",
235+
" self.maintenance = maintenance\n",
216236
"\n",
217-
"school_bus = Bus(\"School Volvo\", 12, 50)\n",
218-
"print(\"Total Bus fare is:\", school_bus.fare()) # Expected output: Total Bus fare is: (calculated amount)"
237+
" def fare(self):\n",
238+
" return super().fare() * (1+self.maintenance)\n",
239+
"\n",
240+
"vehicle = Vehicle(\"School Volvo\", 12, 50)\n",
241+
"school_bus = Bus(\"School Volvo\", 12, 50, 0.1)\n",
242+
"print(\"Total Bus fare is:\", school_bus.fare()) # Expected output: Total Bus fare is: (calculated amount)\n",
243+
"print(\"Total Bus fare is:\", vehicle.fare()) # Expected output: Total Bus fare is: (calculated amount)"
219244
]
220245
},
221246
{
@@ -228,7 +253,7 @@
228253
},
229254
{
230255
"cell_type": "code",
231-
"execution_count": 7,
256+
"execution_count": 12,
232257
"metadata": {},
233258
"outputs": [
234259
{
@@ -255,7 +280,7 @@
255280
},
256281
{
257282
"cell_type": "code",
258-
"execution_count": 8,
283+
"execution_count": 13,
259284
"metadata": {},
260285
"outputs": [
261286
{
@@ -287,7 +312,7 @@
287312
],
288313
"metadata": {
289314
"kernelspec": {
290-
"display_name": ".venv",
315+
"display_name": "base",
291316
"language": "python",
292317
"name": "python3"
293318
},
@@ -301,7 +326,7 @@
301326
"name": "python",
302327
"nbconvert_exporter": "python",
303328
"pygments_lexer": "ipython3",
304-
"version": "3.8.0"
329+
"version": "3.12.7"
305330
}
306331
},
307332
"nbformat": 4,

0 commit comments

Comments
 (0)