Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 54 additions & 23 deletions lab-oop_in_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"130 0\n"
]
}
],
"source": [
"# Your code here\n",
"class Vehicle:\n",
" # Hint: Define __init__ method with parameters for max_speed and mileage\n",
" def __init__(self, max_speed = 130, mileage= 0):\n",
" self.max_speed = max_speed\n",
" self.mileage = mileage\n",
" pass\n",
"\n",
"# Example instantiation\n",
Expand All @@ -56,7 +67,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -69,7 +80,7 @@
],
"source": [
"# Your code here\n",
"class Vehicle:\n",
"class Vehicle: # Here the class is empty : no method, no attribute needed\n",
" pass\n",
"\n",
"# Example instantiation\n",
Expand All @@ -87,7 +98,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -100,12 +111,17 @@
],
"source": [
"# Your code here\n",
"class Vehicle: # Base class with one attribute and a default name\n",
" def __init__(self, name = \"Magic car\"): # Initializes a vehicle with a default name \"Magic car\"\n",
" self.name = name\n",
"\n",
"class Bus(Vehicle):\n",
" pass\n",
" def __init__(self, name = \"Magic bus\"): # Initializes a bus with a default name \"Magic bus\"\n",
" super().__init__(name) # Calls Vehicle's constructor to set the name attribute\n",
"\n",
"# Example instantiation\n",
"school_bus = Bus()\n",
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>"
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>"
]
},
{
Expand All @@ -118,14 +134,14 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Base fare\n"
"Base fare with extra charge\n"
]
}
],
Expand All @@ -135,9 +151,11 @@
" def fare(self):\n",
" return \"Base fare\"\n",
"\n",
"class Bus(Vehicle):\n",
"class Bus(Vehicle): # Bus inherits from Vehicle\n",
" # Hint: Override fare method to include extra charges\n",
" pass\n",
"\n",
" def fare(self): # Override fare method to include extra charge\n",
" return \"Base fare with extra charge\"\n",
"\n",
"school_bus = Bus()\n",
"print(school_bus.fare()) # Expected output: \"Base fare with extra charge\""
Expand All @@ -153,29 +171,40 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 72,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"White\n",
"White\n",
"Yellow\n",
"White\n"
]
}
],
"source": [
"# Your code here\n",
"class Vehicle:\n",
" color = \"White\" # Hint: Define color as a class attribute\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\""
"print(school_bus.color) # Expected output: \"White\"\n",
"\n",
"car = Vehicle(\"Audi\", 220, 15)\n",
"print(car.color) # Expected output: \"White\"\n",
"\n",
"# Example of changing the color for one instance:\n",
"school_bus.color = \"Yellow\"\n",
"print(school_bus.color) # Yellow (new color attribute for this specific instance)\n",
"print(car.color) # White (no change)"
]
},
{
Expand All @@ -188,14 +217,14 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 74,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Bus fare is: 5000\n"
"Total Bus fare is: 5500.0\n"
]
}
],
Expand All @@ -211,8 +240,9 @@
" return self.capacity * 100\n",
"\n",
"class Bus(Vehicle):\n",
" # Hint: Override fare method to include maintenance charge\n",
" pass\n",
" \n",
" def fare(self):\n",
" return super().fare() * 1.10 # Overrides fare method to add a 10% maintenance charge while reusing the base fare from the parent class\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)"
Expand All @@ -228,7 +258,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 76,
"metadata": {},
"outputs": [
{
Expand All @@ -242,7 +272,8 @@
"source": [
"# Your code here\n",
"school_bus = Bus(\"School Volvo\", 12, 50)\n",
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>"
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>\n",
"\n"
]
},
{
Expand All @@ -255,7 +286,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 52,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -287,7 +318,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -301,7 +332,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down