From decb96f0e29ab1080d457ccb90a372fc20d225b1 Mon Sep 17 00:00:00 2001 From: Jef Schroder Aubert Date: Wed, 7 May 2025 15:08:15 +0200 Subject: [PATCH] Completed lab --- lab-oop_in_python.ipynb | 75 +++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/lab-oop_in_python.ipynb b/lab-oop_in_python.ipynb index 3142c5e..ac9e991 100644 --- a/lab-oop_in_python.ipynb +++ b/lab-oop_in_python.ipynb @@ -30,17 +30,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "120 5000\n" + ] + } + ], "source": [ "# Your code here\n", "class Vehicle:\n", " # Hint: Define __init__ method with parameters for max_speed and mileage\n", - " pass\n", + " def __init__(self, max_speed, mileage):\n", + " self.max_speed = max_speed \n", + " self.mileage = mileage\n", "\n", "# Example instantiation\n", - "modelX = Vehicle() # Create an instance of Vehicle\n", + "modelX = Vehicle(120, 5000) # 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 +66,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -87,25 +97,29 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "\n" + "\n", + "80 100 15000\n" ] } ], "source": [ "# Your code here\n", "class Bus(Vehicle):\n", - " pass\n", + " def __init__(self, max_speed, mileage, capacity):\n", + " super().__init__(max_speed, mileage)\n", + " self.capacity = capacity\n", "\n", "# Example instantiation\n", - "school_bus = Bus()\n", - "print(type(school_bus)) # Expected output: " + "school_bus = Bus(80, 15000, 100)\n", + "print(type(school_bus)) # Expected output: \n", + "print(school_bus.max_speed, school_bus.capacity, school_bus.mileage)" ] }, { @@ -118,14 +132,14 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Base fare\n" + "Base fare with extra charge for maintenance is 7€.\n" ] } ], @@ -133,11 +147,17 @@ "# Your code here\n", "class Vehicle:\n", " def fare(self):\n", - " return \"Base fare\"\n", + " base_fare = 5\n", + " return \"Base fare is\", base_fare\n", "\n", "class Bus(Vehicle):\n", + " def fare(self):\n", + " base_fare = 5\n", + " extra_charge = 2\n", + " total_bus_fare = base_fare + extra_charge\n", + " return \"Base fare with extra charge for maintenance is \" + str(total_bus_fare) +\"€.\"\n", " # Hint: Override fare method to include extra charges\n", - " pass\n", + " \n", "\n", "school_bus = Bus()\n", "print(school_bus.fare()) # Expected output: \"Base fare with extra charge\"" @@ -153,29 +173,29 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "White\n" + "Green\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", + " self.color = \"Green\"\n", "\n", "school_bus = Vehicle(\"School Volvo\", 180, 12)\n", - "print(school_bus.color) # Expected output: \"White\"" + "print(school_bus.color) # Expected output: \"Green\"" ] }, { @@ -188,14 +208,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Total Bus fare is: 5000\n" + "Total Bus fare is: 5500.0\n" ] } ], @@ -211,8 +231,11 @@ " 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 * 100 * 1.1\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)" @@ -228,7 +251,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 39, "metadata": {}, "outputs": [ { @@ -255,7 +278,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 43, "metadata": {}, "outputs": [ { @@ -287,7 +310,7 @@ ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -301,7 +324,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.3" } }, "nbformat": 4,