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
73 changes: 45 additions & 28 deletions lab-oop_in_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"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): # Hint: Define __init__ method with parameters for max_speed and 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)"
"print(modelX.max_speed, modelX.mileage) # Expected output: (value of max_speed, value of mileage)\n"
]
},
{
Expand All @@ -56,7 +65,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -87,7 +96,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 23,
"metadata": {},
"outputs": [
{
Expand All @@ -100,12 +109,15 @@
],
"source": [
"# Your code here\n",
"class Bus(Vehicle):\n",
" pass\n",
"class Bus(Vehicle): # bus = class\n",
" def __init__(self, max_speed, mileage): # __init__ = main method, max speed & milage = arguments\n",
" super().__init__() # Call parent with NO arguments\n",
" self.max_speed = max_speed # max speed argument \n",
" self.mileage = mileage # \n",
"\n",
"# Example instantiation\n",
"school_bus = Bus()\n",
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>"
"school_bus = Bus(65,100000) # school bus = object\n",
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>\n"
]
},
{
Expand All @@ -118,14 +130,14 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Base fare\n"
"Base fare with extra charge\n"
]
}
],
Expand All @@ -136,8 +148,10 @@
" return \"Base fare\"\n",
"\n",
"class Bus(Vehicle):\n",
" # Hint: Override fare method to include extra charges\n",
" pass\n",
" def fare(self):\n",
" base_fare = super().fare()\n",
" return f\"{base_fare} with extra charge\"\n",
" \n",
"\n",
"school_bus = Bus()\n",
"print(school_bus.fare()) # Expected output: \"Base fare with extra charge\""
Expand All @@ -153,7 +167,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 29,
"metadata": {},
"outputs": [
{
Expand All @@ -170,9 +184,10 @@
" color = \"White\" # Hint: Define color as a class attribute\n",
"\n",
" def __init__(self, name, max_speed, mileage):\n",
" self.name = name\n",
" self.max_speed = max_speed\n",
" self.mileage = mileage\n",
" self.name = name # attribute \n",
" self.max_speed = max_speed # attribute \n",
" self.mileage = mileage # attribute \n",
" self.color = \"White\"\n",
"\n",
"school_bus = Vehicle(\"School Volvo\", 180, 12)\n",
"print(school_bus.color) # Expected output: \"White\""
Expand All @@ -188,14 +203,14 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Bus fare is: 5000\n"
"Total Bus fare is: 5500.0\n"
]
}
],
Expand All @@ -212,7 +227,9 @@
"\n",
"class Bus(Vehicle):\n",
" # Hint: Override fare method to include maintenance charge\n",
" pass\n",
" def fare(self):\n",
" extra_fare = (super().fare() * 0.1) + super().fare()\n",
" return extra_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 +245,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 32,
"metadata": {},
"outputs": [
{
Expand All @@ -241,7 +258,7 @@
],
"source": [
"# Your code here\n",
"school_bus = Bus(\"School Volvo\", 12, 50)\n",
"school_bus = Bus(\"School Volvo\", 12, 50) # this is our object\n",
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>"
]
},
Expand All @@ -255,7 +272,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 33,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -287,7 +304,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -301,7 +318,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down