|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# LAB | Object-Oriented Programming (OOP) in Python" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "## Overview\n", |
| 15 | + "This exercise notebook will help you practice Object-Oriented Programming concepts in Python. You will create classes, instantiate objects, and use inheritance to build more complex structures.\n", |
| 16 | + "\n", |
| 17 | + "## Instructions\n", |
| 18 | + "- Complete each exercise by writing the appropriate code in the provided space.\n", |
| 19 | + "- Test your code to ensure it works as expected.\n", |
| 20 | + "- Use the hints provided if you get stuck." |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "markdown", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "### Exercise 1: Create a Class with Instance Attributes\n", |
| 28 | + "Write a Python program to create a `Vehicle` class with `max_speed` and `mileage` instance attributes." |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": null, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "# Your code here\n", |
| 38 | + "class Vehicle:\n", |
| 39 | + " # Hint: Define __init__ method with parameters for max_speed and mileage\n", |
| 40 | + " pass\n", |
| 41 | + "\n", |
| 42 | + "# Example instantiation\n", |
| 43 | + "modelX = Vehicle() # Create an instance of Vehicle\n", |
| 44 | + "\n", |
| 45 | + "# Print attributes\n", |
| 46 | + "print(modelX.max_speed, modelX.mileage) # Expected output: (value of max_speed, value of mileage)" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "### Exercise 2: Create a Vehicle Class Without Any Variables and Methods\n", |
| 54 | + "Create a `Vehicle` class without any variables or methods." |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": 2, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [ |
| 62 | + { |
| 63 | + "name": "stdout", |
| 64 | + "output_type": "stream", |
| 65 | + "text": [ |
| 66 | + "<class '__main__.Vehicle'>\n" |
| 67 | + ] |
| 68 | + } |
| 69 | + ], |
| 70 | + "source": [ |
| 71 | + "# Your code here\n", |
| 72 | + "class Vehicle:\n", |
| 73 | + " pass\n", |
| 74 | + "\n", |
| 75 | + "# Example instantiation\n", |
| 76 | + "my_vehicle = Vehicle()\n", |
| 77 | + "print(type(my_vehicle)) # Expected output: <class '__main__.Vehicle'>" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "markdown", |
| 82 | + "metadata": {}, |
| 83 | + "source": [ |
| 84 | + "### Exercise 3: Create a Child Class Bus\n", |
| 85 | + "Create a child class `Bus` that will inherit all of the variables and methods of the `Vehicle` class." |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 3, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [ |
| 93 | + { |
| 94 | + "name": "stdout", |
| 95 | + "output_type": "stream", |
| 96 | + "text": [ |
| 97 | + "<class '__main__.Bus'>\n" |
| 98 | + ] |
| 99 | + } |
| 100 | + ], |
| 101 | + "source": [ |
| 102 | + "# Your code here\n", |
| 103 | + "class Bus(Vehicle):\n", |
| 104 | + " pass\n", |
| 105 | + "\n", |
| 106 | + "# Example instantiation\n", |
| 107 | + "school_bus = Bus()\n", |
| 108 | + "print(type(school_bus)) # Expected output: <class '__main__.Bus'>" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "markdown", |
| 113 | + "metadata": {}, |
| 114 | + "source": [ |
| 115 | + "### Exercise 4: Class Inheritance with Method Overriding\n", |
| 116 | + "Create a `Bus` class that inherits from the `Vehicle` class. Override the `fare()` method to include an extra charge for maintenance." |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "code", |
| 121 | + "execution_count": 4, |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [ |
| 124 | + { |
| 125 | + "name": "stdout", |
| 126 | + "output_type": "stream", |
| 127 | + "text": [ |
| 128 | + "Base fare\n" |
| 129 | + ] |
| 130 | + } |
| 131 | + ], |
| 132 | + "source": [ |
| 133 | + "# Your code here\n", |
| 134 | + "class Vehicle:\n", |
| 135 | + " def fare(self):\n", |
| 136 | + " return \"Base fare\"\n", |
| 137 | + "\n", |
| 138 | + "class Bus(Vehicle):\n", |
| 139 | + " # Hint: Override fare method to include extra charges\n", |
| 140 | + " pass\n", |
| 141 | + "\n", |
| 142 | + "school_bus = Bus()\n", |
| 143 | + "print(school_bus.fare()) # Expected output: \"Base fare with extra charge\"" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "markdown", |
| 148 | + "metadata": {}, |
| 149 | + "source": [ |
| 150 | + "### Exercise 5: Define a Class Attribute\n", |
| 151 | + "Define a property that must have the same value for every class instance (object). Set a default value for `color`." |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "code", |
| 156 | + "execution_count": 5, |
| 157 | + "metadata": {}, |
| 158 | + "outputs": [ |
| 159 | + { |
| 160 | + "name": "stdout", |
| 161 | + "output_type": "stream", |
| 162 | + "text": [ |
| 163 | + "White\n" |
| 164 | + ] |
| 165 | + } |
| 166 | + ], |
| 167 | + "source": [ |
| 168 | + "# Your code here\n", |
| 169 | + "class Vehicle:\n", |
| 170 | + " color = \"White\" # Hint: Define color as a class attribute\n", |
| 171 | + "\n", |
| 172 | + " def __init__(self, name, max_speed, mileage):\n", |
| 173 | + " self.name = name\n", |
| 174 | + " self.max_speed = max_speed\n", |
| 175 | + " self.mileage = mileage\n", |
| 176 | + "\n", |
| 177 | + "school_bus = Vehicle(\"School Volvo\", 180, 12)\n", |
| 178 | + "print(school_bus.color) # Expected output: \"White\"" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "markdown", |
| 183 | + "metadata": {}, |
| 184 | + "source": [ |
| 185 | + "### Exercise 6: Class Inheritance with Default Fare Calculation\n", |
| 186 | + "Create a `Bus` child class that inherits from the `Vehicle` class. The default fare charge of any vehicle is `seating capacity * 100`. If the vehicle is a bus instance, add an extra 10% on the full fare as a maintenance charge." |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "code", |
| 191 | + "execution_count": 6, |
| 192 | + "metadata": {}, |
| 193 | + "outputs": [ |
| 194 | + { |
| 195 | + "name": "stdout", |
| 196 | + "output_type": "stream", |
| 197 | + "text": [ |
| 198 | + "Total Bus fare is: 5000\n" |
| 199 | + ] |
| 200 | + } |
| 201 | + ], |
| 202 | + "source": [ |
| 203 | + "# Your code here\n", |
| 204 | + "class Vehicle:\n", |
| 205 | + " def __init__(self, name, mileage, capacity):\n", |
| 206 | + " self.name = name\n", |
| 207 | + " self.mileage = mileage\n", |
| 208 | + " self.capacity = capacity\n", |
| 209 | + "\n", |
| 210 | + " def fare(self):\n", |
| 211 | + " return self.capacity * 100\n", |
| 212 | + "\n", |
| 213 | + "class Bus(Vehicle):\n", |
| 214 | + " # Hint: Override fare method to include maintenance charge\n", |
| 215 | + " pass\n", |
| 216 | + "\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)" |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "markdown", |
| 223 | + "metadata": {}, |
| 224 | + "source": [ |
| 225 | + "### Exercise 7: Check Type of an Object\n", |
| 226 | + "Write a program to determine which class a given object belongs to." |
| 227 | + ] |
| 228 | + }, |
| 229 | + { |
| 230 | + "cell_type": "code", |
| 231 | + "execution_count": 7, |
| 232 | + "metadata": {}, |
| 233 | + "outputs": [ |
| 234 | + { |
| 235 | + "name": "stdout", |
| 236 | + "output_type": "stream", |
| 237 | + "text": [ |
| 238 | + "<class '__main__.Bus'>\n" |
| 239 | + ] |
| 240 | + } |
| 241 | + ], |
| 242 | + "source": [ |
| 243 | + "# Your code here\n", |
| 244 | + "school_bus = Bus(\"School Volvo\", 12, 50)\n", |
| 245 | + "print(type(school_bus)) # Expected output: <class '__main__.Bus'>" |
| 246 | + ] |
| 247 | + }, |
| 248 | + { |
| 249 | + "cell_type": "markdown", |
| 250 | + "metadata": {}, |
| 251 | + "source": [ |
| 252 | + "### Exercise 8: Check Instance of Class \n", |
| 253 | + "Determine if `school_bus` is also an instance of the `Vehicle` class.\n" |
| 254 | + ] |
| 255 | + }, |
| 256 | + { |
| 257 | + "cell_type": "code", |
| 258 | + "execution_count": 8, |
| 259 | + "metadata": {}, |
| 260 | + "outputs": [ |
| 261 | + { |
| 262 | + "name": "stdout", |
| 263 | + "output_type": "stream", |
| 264 | + "text": [ |
| 265 | + "True\n" |
| 266 | + ] |
| 267 | + } |
| 268 | + ], |
| 269 | + "source": [ |
| 270 | + "# Your code here\n", |
| 271 | + "print(isinstance(school_bus, Vehicle)) # Expected output: True or False based on inheritance" |
| 272 | + ] |
| 273 | + }, |
| 274 | + { |
| 275 | + "cell_type": "markdown", |
| 276 | + "metadata": {}, |
| 277 | + "source": [ |
| 278 | + "### Exercise Completion \n", |
| 279 | + "Once you have completed all exercises:\n", |
| 280 | + "- Review your solutions.\n", |
| 281 | + "- Ensure your code is well-documented with comments explaining your logic.\n", |
| 282 | + "- Save your notebook for submission or further review.\n", |
| 283 | + "\n", |
| 284 | + "Happy coding! Enjoy exploring Object-Oriented Programming with Python!\n" |
| 285 | + ] |
| 286 | + } |
| 287 | + ], |
| 288 | + "metadata": { |
| 289 | + "kernelspec": { |
| 290 | + "display_name": ".venv", |
| 291 | + "language": "python", |
| 292 | + "name": "python3" |
| 293 | + }, |
| 294 | + "language_info": { |
| 295 | + "codemirror_mode": { |
| 296 | + "name": "ipython", |
| 297 | + "version": 3 |
| 298 | + }, |
| 299 | + "file_extension": ".py", |
| 300 | + "mimetype": "text/x-python", |
| 301 | + "name": "python", |
| 302 | + "nbconvert_exporter": "python", |
| 303 | + "pygments_lexer": "ipython3", |
| 304 | + "version": "3.8.0" |
| 305 | + } |
| 306 | + }, |
| 307 | + "nbformat": 4, |
| 308 | + "nbformat_minor": 2 |
| 309 | +} |
0 commit comments