Skip to content

Completed lab exercise #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
64 changes: 49 additions & 15 deletions lab-oop_in_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"180 20\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",
" \"\"\"\n",
" Initializes a new Vehicle instance.\n",
"\n",
" Args:\n",
" max_speed (int): The maximum speed of the vehicle.\n",
" mileage (int): The mileage of the vehicle.\n",
" \"\"\"\n",
" self.max_speed = max_speed\n",
" self.mileage = mileage\n",
"\n",
"\n",
"# Example instantiation\n",
"modelX = Vehicle() # Create an instance of Vehicle\n",
"modelX = Vehicle(180, 20) # 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)"
Expand Down Expand Up @@ -70,7 +88,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 Down Expand Up @@ -101,7 +121,9 @@
"source": [
"# Your code here\n",
"class Bus(Vehicle):\n",
" pass\n",
"\n",
" def __init__(self):\n",
" super().__init__()\n",
"\n",
"# Example instantiation\n",
"school_bus = Bus()\n",
Expand All @@ -125,7 +147,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Base fare\n"
"Base fare with extra charge\n"
]
}
],
Expand All @@ -137,6 +159,8 @@
"\n",
"class Bus(Vehicle):\n",
" # Hint: Override fare method to include extra charges\n",
" def fare(self):\n",
" return \"Base fare with extra charge\"\n",
" pass\n",
"\n",
"school_bus = Bus()\n",
Expand All @@ -153,7 +177,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -188,14 +212,14 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Bus fare is: 5000\n"
"Total Bus fare is: 5500.0\n"
]
}
],
Expand All @@ -212,8 +236,18 @@
"\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",
" # Get the base fare from the parent class\n",
" base_fare = super().fare()\n",
" # Calculate the maintenance charge (10% of base fare)\n",
" maintenance_charge = base_fare * 0.10\n",
" # Total fare is base fare plus maintenance charge\n",
" total_fare = base_fare + maintenance_charge\n",
" return total_fare\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 +262,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand All @@ -255,7 +289,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -287,7 +321,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -301,7 +335,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down