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
113 changes: 74 additions & 39 deletions lab-oop_in_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"130 23\n"
]
}
],
"source": [
"# Your code here\n",
"class Vehicle:\n",
" # Hint: Define __init__ method with parameters for max_speed and mileage\n",
" pass\n",
"\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",
"\n",
"modelX = Vehicle(130,23) # Create an instance of Vehicle\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)"
]
},
{
Expand All @@ -56,7 +64,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 24,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -87,25 +95,23 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class '__main__.Bus'>\n"
]
}
],
"outputs": [],
"source": [
"# Your code here\n",
"class Bus(Vehicle):\n",
" pass\n",
"# Parent class\n",
"class Vehicle:\n",
" def __init__(self, brand, model, year):\n",
" self.brand = brand\n",
" self.model = model\n",
" self.year = year\n",
"\n",
"# Example instantiation\n",
"school_bus = Bus()\n",
"print(type(school_bus)) # Expected output: <class '__main__.Bus'>"
" def display_info(self):\n",
" print(f\"{self.year} {self.brand} {self.model}\")\n",
"\n",
"# Child class\n",
"class Bus(Vehicle):\n",
" pass # Inherits everything from Vehicle\n"
]
},
{
Expand All @@ -118,14 +124,15 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Base fare\n"
"Base fare\n",
"Base fare with extra charge\n"
]
}
],
Expand All @@ -140,7 +147,20 @@
" pass\n",
"\n",
"school_bus = Bus()\n",
"print(school_bus.fare()) # Expected output: \"Base fare with extra charge\""
"print(school_bus.fare()) # Expected output: \"Base fare with extra charge\"\n",
"\n",
"\n",
"class Vehicle:\n",
" def fare(self):\n",
" return \"Base fare\"\n",
"\n",
"class Bus(Vehicle):\n",
" # Override fare method to include extra charges\n",
" def fare(self):\n",
" return super().fare() + \" with extra charge\"\n",
"\n",
"school_bus = Bus()\n",
"print(school_bus.fare()) # Expected output: \"Base fare with extra charge\"\n"
]
},
{
Expand All @@ -153,7 +173,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 41,
"metadata": {},
"outputs": [
{
Expand All @@ -165,17 +185,20 @@
}
],
"source": [
"# Your code here\n",
"\n",
"class Vehicle:\n",
" color = \"White\" # Hint: Define color as a class attribute\n",
" color = \"White\" # Class attribute (shared across all instances)\n",
"\n",
" def __init__(self, name, max_speed, mileage):\n",
" self.name = name\n",
" self.name = name # Instance attribute\n",
" self.max_speed = max_speed\n",
" self.mileage = mileage\n",
"\n",
"# Create an instance\n",
"school_bus = Vehicle(\"School Volvo\", 180, 12)\n",
"print(school_bus.color) # Expected output: \"White\""
"\n",
"# Access the class attribute\n",
"print(school_bus.color) # Output: \"White\"\n"
]
},
{
Expand All @@ -188,7 +211,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 36,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -228,7 +251,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 37,
"metadata": {},
"outputs": [
{
Expand All @@ -255,7 +278,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 43,
"metadata": {},
"outputs": [
{
Expand All @@ -268,7 +291,19 @@
],
"source": [
"# Your code here\n",
"print(isinstance(school_bus, Vehicle)) # Expected output: True or False based on inheritance"
"print(isinstance(school_bus, Vehicle)) # Expected output: True or False based on inheritance\n",
"\n",
"class Vehicle:\n",
" def __init__(self, name, seating_capacity):\n",
" self.name = name\n",
" self.seating_capacity = seating_capacity\n",
"\n",
"class Bus(Vehicle):\n",
" pass\n",
"\n",
"# Create an instance of Bus\n",
"school_bus = Bus(\"School Volvo\", 50)\n",
"\n"
]
},
{
Expand All @@ -287,7 +322,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -301,7 +336,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down