From 5d1211c02071c92126eb555a2dbc38b60362aaa2 Mon Sep 17 00:00:00 2001 From: Nadav Smith Date: Sat, 13 Sep 2025 15:10:58 +0200 Subject: [PATCH] solve oop lab --- lab-oop_in_python.ipynb | 68 +++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/lab-oop_in_python.ipynb b/lab-oop_in_python.ipynb index 3142c5e..866ec50 100644 --- a/lab-oop_in_python.ipynb +++ b/lab-oop_in_python.ipynb @@ -30,14 +30,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200 10000\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=200, mileage=10000):\n", + " self.max_speed = max_speed\n", + " self.mileage = mileage\n", "\n", "# Example instantiation\n", "modelX = Vehicle() # Create an instance of Vehicle\n", @@ -56,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -70,7 +80,10 @@ "source": [ "# Your code here\n", "class Vehicle:\n", - " pass\n", + " # Hint: Define __init__ method with parameters for max_speed and mileage\n", + " def __init__(self):\n", + " pass\n", + " \n", "\n", "# Example instantiation\n", "my_vehicle = Vehicle()\n", @@ -87,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -101,7 +114,8 @@ "source": [ "# Your code here\n", "class Bus(Vehicle):\n", - " pass\n", + " def __init__(self):\n", + " super().__init__()\n", "\n", "# Example instantiation\n", "school_bus = Bus()\n", @@ -118,14 +132,14 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Base fare\n" + "Base fare with extra charge\n" ] } ], @@ -137,7 +151,9 @@ "\n", "class Bus(Vehicle):\n", " # Hint: Override fare method to include extra charges\n", - " pass\n", + " def fare(self):\n", + " base_fare = super().fare()\n", + " return f\"{base_fare} with extra charge\"\n", "\n", "school_bus = Bus()\n", "print(school_bus.fare()) # Expected output: \"Base fare with extra charge\"" @@ -153,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -173,6 +189,7 @@ " self.name = name\n", " self.max_speed = max_speed\n", " self.mileage = mileage\n", + " self.color = Vehicle.color \n", "\n", "school_bus = Vehicle(\"School Volvo\", 180, 12)\n", "print(school_bus.color) # Expected output: \"White\"" @@ -188,14 +205,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 +229,13 @@ "\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", + " base_fare = super().fare()\n", + " maintenance_charge = base_fare * 0.10\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,21 +251,28 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "\n" + "\n", + "Bus\n" ] } ], "source": [ "# Your code here\n", "school_bus = Bus(\"School Volvo\", 12, 50)\n", - "print(type(school_bus)) # Expected output: " + "print(type(school_bus)) # Expected output: \n", + "\n", + "# Program to determine the class of an object\n", + "def get_class_name(arg):\n", + " return arg.__class__.__name__\n", + "\n", + "print(get_class_name(school_bus)) # Expected output: \"Bus\"" ] }, { @@ -255,7 +285,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -301,7 +331,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.11.6" } }, "nbformat": 4,