diff --git a/lab-oop_in_python.ipynb b/lab-oop_in_python.ipynb index 3142c5e..4d795d9 100644 --- a/lab-oop_in_python.ipynb +++ b/lab-oop_in_python.ipynb @@ -1,309 +1,366 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# LAB | Object-Oriented Programming (OOP) in Python" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Overview\n", - "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", - "\n", - "## Instructions\n", - "- Complete each exercise by writing the appropriate code in the provided space.\n", - "- Test your code to ensure it works as expected.\n", - "- Use the hints provided if you get stuck." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 1: Create a Class with Instance Attributes\n", - "Write a Python program to create a `Vehicle` class with `max_speed` and `mileage` instance attributes." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n", - "class Vehicle:\n", - " # Hint: Define __init__ method with parameters for max_speed and mileage\n", - " pass\n", - "\n", - "# Example instantiation\n", - "modelX = Vehicle() # Create an instance of Vehicle\n", - "\n", - "# Print attributes\n", - "print(modelX.max_speed, modelX.mileage) # Expected output: (value of max_speed, value of mileage)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 2: Create a Vehicle Class Without Any Variables and Methods\n", - "Create a `Vehicle` class without any variables or methods." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ + "cells": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "source": [ - "# Your code here\n", - "class Vehicle:\n", - " pass\n", - "\n", - "# Example instantiation\n", - "my_vehicle = Vehicle()\n", - "print(type(my_vehicle)) # Expected output: " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 3: Create a Child Class Bus\n", - "Create a child class `Bus` that will inherit all of the variables and methods of the `Vehicle` class." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ + "cell_type": "markdown", + "metadata": { + "id": "ER0PED4rOASy" + }, + "source": [ + "# LAB | Object-Oriented Programming (OOP) in Python" + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "source": [ - "# Your code here\n", - "class Bus(Vehicle):\n", - " pass\n", - "\n", - "# Example instantiation\n", - "school_bus = Bus()\n", - "print(type(school_bus)) # Expected output: " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 4: Class Inheritance with Method Overriding\n", - "Create a `Bus` class that inherits from the `Vehicle` class. Override the `fare()` method to include an extra charge for maintenance." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ + "cell_type": "markdown", + "metadata": { + "id": "W0uF_qjbOAS0" + }, + "source": [ + "## Overview\n", + "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", + "\n", + "## Instructions\n", + "- Complete each exercise by writing the appropriate code in the provided space.\n", + "- Test your code to ensure it works as expected.\n", + "- Use the hints provided if you get stuck." + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "Base fare\n" - ] - } - ], - "source": [ - "# Your code here\n", - "class Vehicle:\n", - " def fare(self):\n", - " return \"Base fare\"\n", - "\n", - "class Bus(Vehicle):\n", - " # Hint: Override fare method to include extra charges\n", - " pass\n", - "\n", - "school_bus = Bus()\n", - "print(school_bus.fare()) # Expected output: \"Base fare with extra charge\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 5: Define a Class Attribute\n", - "Define a property that must have the same value for every class instance (object). Set a default value for `color`." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ + "cell_type": "markdown", + "metadata": { + "id": "eU85J9DVOAS0" + }, + "source": [ + "### Exercise 1: Create a Class with Instance Attributes\n", + "Write a Python program to create a `Vehicle` class with `max_speed` and `mileage` instance attributes." + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "White\n" - ] - } - ], - "source": [ - "# Your code here\n", - "class Vehicle:\n", - " color = \"White\" # Hint: Define color as a class attribute\n", - "\n", - " def __init__(self, name, max_speed, mileage):\n", - " self.name = name\n", - " self.max_speed = max_speed\n", - " self.mileage = mileage\n", - "\n", - "school_bus = Vehicle(\"School Volvo\", 180, 12)\n", - "print(school_bus.color) # Expected output: \"White\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 6: Class Inheritance with Default Fare Calculation\n", - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "7dd845fAOAS1" + }, + "outputs": [], + "source": [ + "# Exercise 1\n", + "class Vehicle:\n", + " # Constructor with instance attributes\n", + " def __init__(self, max_speed, mileage):\n", + " self.max_speed = max_speed\n", + " self.mileage = mileage\n", + "\n", + "# Example instantiation\n", + "modelX = Vehicle(240, 18)\n", + "\n", + "# Print attributes\n", + "print(modelX.max_speed, modelX.mileage) # Expected output: 240 18\n" + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "Total Bus fare is: 5000\n" - ] - } - ], - "source": [ - "# Your code here\n", - "class Vehicle:\n", - " def __init__(self, name, mileage, capacity):\n", - " self.name = name\n", - " self.mileage = mileage\n", - " self.capacity = capacity\n", - "\n", - " def fare(self):\n", - " return self.capacity * 100\n", - "\n", - "class Bus(Vehicle):\n", - " # Hint: Override fare method to include maintenance charge\n", - " pass\n", - "\n", - "school_bus = Bus(\"School Volvo\", 12, 50)\n", - "print(\"Total Bus fare is:\", school_bus.fare()) # Expected output: Total Bus fare is: (calculated amount)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 7: Check Type of an Object\n", - "Write a program to determine which class a given object belongs to." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ + "cell_type": "markdown", + "metadata": { + "id": "RBgwBNHuOAS1" + }, + "source": [ + "### Exercise 2: Create a Vehicle Class Without Any Variables and Methods\n", + "Create a `Vehicle` class without any variables or methods." + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "source": [ - "# Your code here\n", - "school_bus = Bus(\"School Volvo\", 12, 50)\n", - "print(type(school_bus)) # Expected output: " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise 8: Check Instance of Class \n", - "Determine if `school_bus` is also an instance of the `Vehicle` class.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "ROko3466OAS2", + "outputId": "5fed7e0a-0077-43b4-ccc0-0472f3f31d94" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# Exercise 2\n", + "class Vehicle:\n", + " pass\n", + "\n", + "# Example instantiation\n", + "my_vehicle = Vehicle()\n", + "print(type(my_vehicle)) # Expected output: \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "P0E-4xDeOAS2" + }, + "source": [ + "### Exercise 3: Create a Child Class Bus\n", + "Create a child class `Bus` that will inherit all of the variables and methods of the `Vehicle` class." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "_2BZAtdOOAS2", + "outputId": "4d402ddb-0e7a-43ab-9a71-b59f9106e299" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# Exercise 3\n", + "class Vehicle:\n", + " pass\n", + "\n", + "class Bus(Vehicle):\n", + " pass\n", + "\n", + "# Example instantiation\n", + "school_bus = Bus()\n", + "print(type(school_bus)) # Expected output: \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ly02X4vaOAS3" + }, + "source": [ + "### Exercise 4: Class Inheritance with Method Overriding\n", + "Create a `Bus` class that inherits from the `Vehicle` class. Override the `fare()` method to include an extra charge for maintenance." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "6X5jWT3xOAS3", + "outputId": "7ade6c71-5536-41f8-b754-7a0a0ab88fbd" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Base fare\n" + ] + } + ], + "source": [ + "# Exercise 4\n", + "class Vehicle:\n", + " def fare(self):\n", + " return \"Base fare\"\n", + "\n", + "class Bus(Vehicle):\n", + " # Override fare method\n", + " def fare(self):\n", + " return super().fare() + \" with extra charge\"\n", + "\n", + "school_bus = Bus()\n", + "print(school_bus.fare()) # Expected output: \"Base fare with extra charge\"\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jA_V0bxvOAS3" + }, + "source": [ + "### Exercise 5: Define a Class Attribute\n", + "Define a property that must have the same value for every class instance (object). Set a default value for `color`." + ] + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "ISZdRMJ8OAS3", + "outputId": "9b97a9b5-871a-4463-e98f-a18090cd6a62" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "White\n" + ] + } + ], + "source": [ + "# Exercise 5\n", + "class Vehicle:\n", + " color = \"White\" # Class attribute shared by all instances\n", + "\n", + " def __init__(self, name, max_speed, mileage):\n", + " self.name = name\n", + " self.max_speed = max_speed\n", + " self.mileage = mileage\n", + "\n", + "school_bus = Vehicle(\"School Volvo\", 180, 12)\n", + "print(school_bus.color) # Expected output: \"White\"\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OFea9qJLOAS3" + }, + "source": [ + "### Exercise 6: Class Inheritance with Default Fare Calculation\n", + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "V8wm20tdOAS4", + "outputId": "088c7351-3ba0-42be-ce30-2ee562fc9b51" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Bus fare is: 5000\n" + ] + } + ], + "source": [ + "# Exercise 6\n", + "class Vehicle:\n", + " def __init__(self, name, mileage, capacity):\n", + " self.name = name\n", + " self.mileage = mileage\n", + " self.capacity = capacity\n", + "\n", + " def fare(self):\n", + " return self.capacity * 100\n", + "\n", + "class Bus(Vehicle):\n", + " # Override fare method\n", + " def fare(self):\n", + " base_fare = super().fare()\n", + " total_fare = base_fare + (0.1 * base_fare) # Add 10% maintenance charge\n", + " return total_fare\n", + "\n", + "school_bus = Bus(\"School Volvo\", 12, 50)\n", + "print(\"Total Bus fare is:\", school_bus.fare()) # Expected: Total Bus fare is: 5500.0\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SmcUV2wrOAS4" + }, + "source": [ + "### Exercise 7: Check Type of an Object\n", + "Write a program to determine which class a given object belongs to." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "-90A0F6SOAS4", + "outputId": "a0a7b35d-bfa4-4433-f9c7-83118ae6991f" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# Exercise 7\n", + "school_bus = Bus(\"School Volvo\", 12, 50)\n", + "print(type(school_bus)) # Expected output: \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_hlIS98hOAS4" + }, + "source": [ + "### Exercise 8: Check Instance of Class\n", + "Determine if `school_bus` is also an instance of the `Vehicle` class.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "8sh_tVwaOAS4", + "outputId": "7a58acdf-5e6b-4c4d-cbc9-e4feb2de12bb" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "# Exercise 8\n", + "print(isinstance(school_bus, Vehicle)) # Expected output: True\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "loxB2EXOOAS4" + }, + "source": [ + "### Exercise Completion \n", + "Once you have completed all exercises:\n", + "- Review your solutions.\n", + "- Ensure your code is well-documented with comments explaining your logic.\n", + "- Save your notebook for submission or further review.\n", + "\n", + "Happy coding! Enjoy exploring Object-Oriented Programming with Python!\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.0" + }, + "colab": { + "provenance": [] } - ], - "source": [ - "# Your code here\n", - "print(isinstance(school_bus, Vehicle)) # Expected output: True or False based on inheritance" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercise Completion \n", - "Once you have completed all exercises:\n", - "- Review your solutions.\n", - "- Ensure your code is well-documented with comments explaining your logic.\n", - "- Save your notebook for submission or further review.\n", - "\n", - "Happy coding! Enjoy exploring Object-Oriented Programming with Python!\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": ".venv", - "language": "python", - "name": "python3" }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.0" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file