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
74 changes: 50 additions & 24 deletions lab-oop_in_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"250 5\n"
]
}
],
"source": [
"# Your code here\n",
"class Vehicle:\n",
" # Hint: Define __init__ method with parameters for max_speed and mileage\n",
" pass\n",
"# Constructor\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(250,5) # Create an instance of Vehicle\n",
"def __init__(self, max_speed, mileage):\n",
" super().__init__(max_speed, mileage)\n",
"\n",
"# Print attributes\n",
"print(modelX.max_speed, modelX.mileage) # Expected output: (value of max_speed, value of mileage)"
Expand All @@ -56,7 +70,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 19,
"metadata": {},
"outputs": [
{
Expand All @@ -70,7 +84,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",
Expand All @@ -87,7 +103,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 21,
"metadata": {},
"outputs": [
{
Expand All @@ -101,7 +117,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",
Expand All @@ -118,14 +135,14 @@
},
{
"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"
]
}
],
Expand All @@ -137,7 +154,12 @@
"\n",
"class Bus(Vehicle):\n",
" # Hint: Override fare method to include extra charges\n",
" pass\n",
" def __init__(self):\n",
" super().__init__()\n",
" \n",
" def fare(self):\n",
" # Overrides the parent method and adds extra charges\n",
" return super().fare() + \" with extra charge\"\n",
"\n",
"school_bus = Bus()\n",
"print(school_bus.fare()) # Expected output: \"Base fare with extra charge\""
Expand All @@ -153,14 +175,14 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"White\n"
"white\n"
]
}
],
Expand All @@ -169,12 +191,13 @@
"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, color):\n",
" self.name = name\n",
" self.max_speed = max_speed\n",
" self.mileage = mileage\n",
" self.color = color\n",
"\n",
"school_bus = Vehicle(\"School Volvo\", 180, 12)\n",
"school_bus = Vehicle(\"School Volvo\", 180, 12, \"white\")\n",
"print(school_bus.color) # Expected output: \"White\""
]
},
Expand All @@ -188,14 +211,14 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"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 +234,9 @@
" return self.capacity * 100\n",
"\n",
"class Bus(Vehicle):\n",
" # Hint: Override fare method to include maintenance charge\n",
" pass\n",
" # Hint: Overrides fare method to include maintenance charge\n",
" def fare(self):\n",
" return super().fare() * 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)"
Expand All @@ -228,21 +252,23 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class '__main__.Bus'>\n"
"<class '__main__.Bus'>\n",
"True\n"
]
}
],
"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",
"print(isinstance(school_bus, Bus))"
]
},
{
Expand All @@ -255,7 +281,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 47,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -287,7 +313,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -301,7 +327,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down