Skip to content

completes lab #98

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
89 changes: 68 additions & 21 deletions lab-oop_in_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
"- Use the hints provided if you get stuck."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -32,15 +39,24 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"120 0\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",
" self.max_speed = max_speed\n",
" self.mileage = mileage \n",
"\n",
"# Example instantiation\n",
"modelX = Vehicle() # Create an instance of Vehicle\n",
"modelX = Vehicle(120, 0) # 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 +72,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -70,7 +86,8 @@
"source": [
"# Your code here\n",
"class Vehicle:\n",
" pass\n",
" def __init__(self):\n",
" pass\n",
"\n",
"# Example instantiation\n",
"my_vehicle = Vehicle()\n",
Expand All @@ -87,7 +104,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -101,10 +118,13 @@
"source": [
"# Your code here\n",
"class Bus(Vehicle):\n",
" pass\n",
"\n",
" def __init__(self, max_speed, mileage):\n",
" self.max_speed = max_speed\n",
" self.mileage = mileage \n",
" super().__init__(max_speed, mileage)\n",
" \n",
"# Example instantiation\n",
"school_bus = Bus()\n",
"school_bus = Bus(60, 10000)\n",
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>"
]
},
Expand All @@ -118,26 +138,39 @@
},
{
"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",
"class Vehicle:\n",
" def __init__(self, max_speed, mileage):\n",
" self.max_speed = max_speed\n",
" self.mileage = mileage \n",
"\n",
" def fare(self):\n",
" return \"Base fare\"\n",
"\n",
"class Bus(Vehicle):\n",
" def __init__(self, max_speed, mileage):\n",
" self.max_speed = max_speed\n",
" self.mileage = mileage \n",
"\n",
" def fare(self):\n",
" super().fare() \n",
" return \"Base fare with extra charge\"\n",
" \n",
"\n",
" # Hint: Override fare method to include extra charges\n",
" pass\n",
" \n",
"\n",
"school_bus = Bus()\n",
"print(school_bus.fare()) # Expected output: \"Base fare with extra charge\""
Expand All @@ -153,7 +186,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 13,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -188,14 +221,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: \"Total Bus fare is:\" 5.0\n"
]
}
],
Expand All @@ -211,8 +244,16 @@
" return self.capacity * 100\n",
"\n",
"class Bus(Vehicle):\n",
" def __init__(self, name, mileage, capacity):\n",
" self.name = name\n",
" self.mileage = mileage\n",
" self.capacity = capacity\n",
"\n",
" def fare(self):\n",
" super().fare() \n",
" return f'\"Total Bus fare is:\" {self.capacity * .10}' \n",
" # Hint: Override fare method to include maintenance charge\n",
" pass\n",
"\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 +269,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand All @@ -255,7 +296,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 18,
"metadata": {},
"outputs": [
{
Expand All @@ -268,6 +309,12 @@
],
"source": [
"# Your code here\n",
"def isinstance(obj, class_name):\n",
" if type(obj).__name__ == \"Bus\":\n",
" return True\n",
" else:\n",
" return False\n",
"\n",
"print(isinstance(school_bus, Vehicle)) # Expected output: True or False based on inheritance"
]
},
Expand All @@ -287,7 +334,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -301,7 +348,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down