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
96 changes: 70 additions & 26 deletions lab-oop_in_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"280 1800\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",
" # Hint: Define __init__ method with parameters for max_speed and mileage\n",
" pass\n",
"\n",
"# Example instantiation\n",
"modelX = Vehicle() # Create an instance of Vehicle\n",
"modelX = Vehicle(280 , 1800) # 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": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -70,7 +82,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 +101,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand All @@ -100,11 +114,15 @@
],
"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",
"class Bus(Vehicle):\n",
" pass\n",
"\n",
" def __init__(self, max_speed, mileage):\n",
" super().__init__(max_speed,mileage)\n",
"# Example instantiation\n",
"school_bus = Bus()\n",
"school_bus = Bus(120,20000)\n",
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>"
]
},
Expand All @@ -118,28 +136,36 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 11,
"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",
" super().__init__(max_speed,mileage)\n",
" def fare(self):\n",
" return 'base fare with extra charge'\n",
" # Hint: Override fare method to include extra charges\n",
" pass\n",
"\n",
"school_bus = Bus()\n",
"\n",
"school_bus = Bus(120,10000)\n",
"print(school_bus.fare()) # Expected output: \"Base fare with extra charge\""
]
},
Expand All @@ -153,7 +179,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 12,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -188,14 +214,14 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Bus fare is: 5000\n"
"Total Bus fare is: 5500 with the 10% extra charge for maintenance\n"
]
}
],
Expand All @@ -211,11 +237,15 @@
" return self.capacity * 100\n",
"\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",
" return self.capacity * 110\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)"
"print(\"Total Bus fare is:\", school_bus.fare(),\"with the 10% extra charge for maintenance\") # Expected output: Total Bus fare is: (calculated amount)"
]
},
{
Expand All @@ -228,7 +258,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 30,
"metadata": {},
"outputs": [
{
Expand All @@ -241,7 +271,12 @@
],
"source": [
"# Your code here\n",
"school_bus = Bus(\"School Volvo\", 12, 50)\n",
"class Vehicle():\n",
" pass\n",
"class Bus(Vehicle):\n",
" pass\n",
"\n",
"school_bus = Bus()\n",
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>"
]
},
Expand All @@ -255,20 +290,29 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
"True\n",
"False\n"
]
}
],
"source": [
"# Your code here\n",
"print(isinstance(school_bus, Vehicle)) # Expected output: True or False based on inheritance"
"class Vehicle():\n",
" pass\n",
"class Bus(Vehicle):\n",
" pass\n",
"class Car():\n",
" pass\n",
"school_bus = Bus()\n",
"print(isinstance(school_bus,Bus)) # Expected output: True or False based on inheritance\n",
"print(isinstance(school_bus,Car)) # Expected output: True or False based on inheritance"
]
},
{
Expand All @@ -287,7 +331,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -301,7 +345,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down