From af80c0d65be7fb80304f6fd6fbf7e5cfa3a69526 Mon Sep 17 00:00:00 2001 From: perlati Date: Wed, 8 Oct 2025 16:26:44 +0200 Subject: [PATCH] # LAB | Object-Oriented Programming (OOP) in Python --- lab-oop_in_python.ipynb | 66 +++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/lab-oop_in_python.ipynb b/lab-oop_in_python.ipynb index 3142c5e..007650b 100644 --- a/lab-oop_in_python.ipynb +++ b/lab-oop_in_python.ipynb @@ -32,18 +32,27 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "240 18\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): # Hint: Add constructor with parameters\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(240, 18) # 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)" + "print(modelX.max_speed, modelX.mileage) # Expected output: 240 18" ] }, { @@ -56,7 +65,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -70,10 +79,11 @@ "source": [ "# Your code here\n", "class Vehicle:\n", - " pass\n", + " def __init__(self): # Hint: Add a constructor\n", + " pass\n", "\n", "# Example instantiation\n", - "my_vehicle = Vehicle()\n", + "my_vehicle = Vehicle() # Create an instance of Vehicle\n", "print(type(my_vehicle)) # Expected output: " ] }, @@ -87,7 +97,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -100,8 +110,10 @@ ], "source": [ "# Your code here\n", - "class Bus(Vehicle):\n", - " pass\n", + "class Bus(Vehicle): # Hint: Create a Bus class that inherits from Vehicle\n", + " def __init__(self): # Hint: Add a constructor\n", + " super().__init__() # Call the constructor of the parent class\n", + " pass\n", "\n", "# Example instantiation\n", "school_bus = Bus()\n", @@ -118,26 +130,27 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "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 fare(self):\n", + " def fare(self): # Hint: Create a method named fare\n", " return \"Base fare\"\n", "\n", "class Bus(Vehicle):\n", - " # Hint: Override fare method to include extra charges\n", - " pass\n", + "\n", + " def fare(self):\n", + " return \"Base fare with extra charge\"\n", "\n", "school_bus = Bus()\n", "print(school_bus.fare()) # Expected output: \"Base fare with extra charge\"" @@ -153,7 +166,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -169,7 +182,7 @@ "class Vehicle:\n", " color = \"White\" # Hint: Define color as a class attribute\n", "\n", - " def __init__(self, name, max_speed, mileage):\n", + " def __init__(self, name, max_speed, mileage): # Hint: Add constructor with parameters\n", " self.name = name\n", " self.max_speed = max_speed\n", " self.mileage = mileage\n", @@ -188,14 +201,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Total Bus fare is: 5000\n" + "Total Bus fare is: 5500.0\n" ] } ], @@ -212,7 +225,10 @@ "\n", "class Bus(Vehicle):\n", " # Hint: Override fare method to include maintenance charge\n", - " pass\n", + " def fare(self):\n", + " base_fare = super().fare() # Get the base fare from parent class\n", + " maintenance_charge = base_fare * 0.1 # Calculate 10% maintenance charge\n", + " return base_fare + maintenance_charge\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 +244,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -255,7 +271,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -287,7 +303,7 @@ ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "base", "language": "python", "name": "python3" }, @@ -301,7 +317,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.12.8" } }, "nbformat": 4,