Skip to content

Week 2, Day 2 Lab: OOP in Python Answers #78

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: 39 additions & 25 deletions lab-oop_in_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,27 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"200 10\n"
]
}
],
"source": [
"# Your code here\n",
"class Vehicle:\n",
" # Hint: Define __init__ method with parameters for max_speed and mileage\n",
" # Define __init__ method with parameters for model, max_speed and mileage\n",
" def __init__(self, model, max_speed, mileage):\n",
" self.model = model\n",
" self.max_speed = max_speed\n",
" self.mileage = mileage \n",
"\n",
" pass\n",
"\n",
"# Example instantiation\n",
"modelX = Vehicle() # Create an instance of Vehicle\n",
"modelX = Vehicle(\"A1\",200, 10 ) # 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 All @@ -56,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -68,7 +80,7 @@
}
],
"source": [
"# Your code here\n",
"\n",
"class Vehicle:\n",
" pass\n",
"\n",
Expand All @@ -87,7 +99,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -99,7 +111,7 @@
}
],
"source": [
"# Your code here\n",
"\n",
"class Bus(Vehicle):\n",
" pass\n",
"\n",
Expand All @@ -118,25 +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",
"\n",
"class Vehicle:\n",
" def fare(self):\n",
" return \"Base fare\"\n",
"\n",
"class Bus(Vehicle):\n",
" # Hint: Override fare method to include extra charges\n",
" # Override fare method to include extra charge comment, child will override parent\n",
" def fare(self):\n",
" return \"Base fare with extra charge\"\n",
" pass\n",
"\n",
"school_bus = Bus()\n",
Expand All @@ -153,7 +167,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -165,9 +179,9 @@
}
],
"source": [
"# Your code here\n",
"\n",
"class Vehicle:\n",
" color = \"White\" # Hint: Define color as a class attribute\n",
" color = \"White\" # Define color as a class attribute\n",
"\n",
" def __init__(self, name, max_speed, mileage):\n",
" self.name = name\n",
Expand All @@ -188,19 +202,19 @@
},
{
"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"
]
}
],
"source": [
"# Your code here\n",
"\n",
"class Vehicle:\n",
" def __init__(self, name, mileage, capacity):\n",
" self.name = name\n",
Expand All @@ -211,7 +225,9 @@
" return self.capacity * 100\n",
"\n",
"class Bus(Vehicle):\n",
" # Hint: Override fare method to include maintenance charge\n",
" # Override fare method to include maintenance charge of 10%\n",
" def fare(self):\n",
" return (self.capacity * 100)*1.1\n",
" pass\n",
"\n",
"school_bus = Bus(\"School Volvo\", 12, 50)\n",
Expand All @@ -228,7 +244,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -240,7 +256,6 @@
}
],
"source": [
"# Your code here\n",
"school_bus = Bus(\"School Volvo\", 12, 50)\n",
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>"
]
Expand All @@ -255,7 +270,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -267,7 +282,6 @@
}
],
"source": [
"# Your code here\n",
"print(isinstance(school_bus, Vehicle)) # Expected output: True or False based on inheritance"
]
},
Expand All @@ -287,7 +301,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -301,7 +315,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down