From 80c86a39682cb2c15255f057b7045b009b9005a5 Mon Sep 17 00:00:00 2001 From: ncodexz Date: Sat, 20 Sep 2025 09:21:58 +0200 Subject: [PATCH] OOP_done --- lab-oop_in_python.ipynb | 96 ++++++++++++++++++++++++++++++----------- 1 file changed, 70 insertions(+), 26 deletions(-) diff --git a/lab-oop_in_python.ipynb b/lab-oop_in_python.ipynb index 3142c5e..0f48d22 100644 --- a/lab-oop_in_python.ipynb +++ b/lab-oop_in_python.ipynb @@ -30,17 +30,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "280 1800\n" + ] + } + ], "source": [ "# Your code here\n", "class Vehicle:\n", + " def __init__(self, max_speed , mileage):\n", + " self.max_speed = max_speed\n", + " self.mileage = mileage\n", + " \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", + "modelX = Vehicle(280 , 1800) # 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)" @@ -56,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -70,7 +82,9 @@ "source": [ "# Your code here\n", "class Vehicle:\n", - " pass\n", + " def __init__(self):\n", + " pass\n", + "\n", "\n", "# Example instantiation\n", "my_vehicle = Vehicle()\n", @@ -87,7 +101,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -100,11 +114,15 @@ ], "source": [ "# Your code here\n", + "class Vehicle:\n", + " def __init__(self, max_speed , mileage):\n", + " self.max_speed = max_speed\n", + " self.mileage = mileage\n", "class Bus(Vehicle):\n", - " pass\n", - "\n", + " def __init__(self, max_speed, mileage):\n", + " super().__init__(max_speed,mileage)\n", "# Example instantiation\n", - "school_bus = Bus()\n", + "school_bus = Bus(120,20000)\n", "print(type(school_bus)) # Expected output: " ] }, @@ -118,28 +136,36 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Base fare\n" + "base fare with extra charge\n" ] } ], "source": [ "# Your code here\n", "class Vehicle:\n", + " def __init__(self, max_speed , mileage):\n", + " self.max_speed = max_speed\n", + " self.mileage = mileage\n", + " \n", " def fare(self):\n", " return \"Base fare\"\n", "\n", "class Bus(Vehicle):\n", + " def __init__(self, max_speed, mileage):\n", + " super().__init__(max_speed,mileage)\n", + " def fare(self):\n", + " return 'base fare with extra charge'\n", " # Hint: Override fare method to include extra charges\n", - " pass\n", "\n", - "school_bus = Bus()\n", + "\n", + "school_bus = Bus(120,10000)\n", "print(school_bus.fare()) # Expected output: \"Base fare with extra charge\"" ] }, @@ -153,7 +179,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -188,14 +214,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Total Bus fare is: 5000\n" + "Total Bus fare is: 5500 with the 10% extra charge for maintenance\n" ] } ], @@ -211,11 +237,15 @@ " return self.capacity * 100\n", "\n", "class Bus(Vehicle):\n", - " # Hint: Override fare method to include maintenance charge\n", - " pass\n", + " def __init__(self, name, mileage, capacity):\n", + " super().__init__(name, mileage, capacity)\n", + " \n", + " def fare(self):\n", + " return self.capacity * 110\n", + " \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)" + "print(\"Total Bus fare is:\", school_bus.fare(),\"with the 10% extra charge for maintenance\") # Expected output: Total Bus fare is: (calculated amount)" ] }, { @@ -228,7 +258,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 30, "metadata": {}, "outputs": [ { @@ -241,7 +271,12 @@ ], "source": [ "# Your code here\n", - "school_bus = Bus(\"School Volvo\", 12, 50)\n", + "class Vehicle():\n", + " pass\n", + "class Bus(Vehicle):\n", + " pass\n", + "\n", + "school_bus = Bus()\n", "print(type(school_bus)) # Expected output: " ] }, @@ -255,20 +290,29 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "True\n" + "True\n", + "False\n" ] } ], "source": [ "# Your code here\n", - "print(isinstance(school_bus, Vehicle)) # Expected output: True or False based on inheritance" + "class Vehicle():\n", + " pass\n", + "class Bus(Vehicle):\n", + " pass\n", + "class Car():\n", + " pass\n", + "school_bus = Bus()\n", + "print(isinstance(school_bus,Bus)) # Expected output: True or False based on inheritance\n", + "print(isinstance(school_bus,Car)) # Expected output: True or False based on inheritance" ] }, { @@ -287,7 +331,7 @@ ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "base", "language": "python", "name": "python3" }, @@ -301,7 +345,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.5" } }, "nbformat": 4,