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
66 changes: 43 additions & 23 deletions lab-oop_in_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,32 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"70 100\n"
]
}
],
"source": [
"# Your code here\n",
"class Vehicle:\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",
" description = \"This is a car\"\n",
" # Constructor: Define __init__ method with parameters for max_speed and mileage\n",
" def __init__(self, max_speed, mileage):\n",
" self.max_speed = max_speed\n",
" self.mileage = mileage\n",
"\n",
"# Example instantiation\n",
"# Create an instance of Vehicle\n",
"class modelX (Vehicle):\n",
" max_speed = 70\n",
" mileage = 100\n",
"# Print attributes\n",
"print(modelX.max_speed, modelX.mileage) # Expected output: (value of max_speed, value of mileage)"
]
Expand All @@ -56,7 +70,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -87,7 +101,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -118,14 +132,14 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Base fare\n"
"Base fare with extra charge: 10\n"
]
}
],
Expand All @@ -136,11 +150,15 @@
" return \"Base fare\"\n",
"\n",
"class Bus(Vehicle):\n",
" # Hint: Override fare method to include extra charges\n",
" pass\n",
" def __init__(self, bus_fare):\n",
" self.bus_fare = bus_fare\n",
"\n",
"school_bus = Bus()\n",
"print(school_bus.fare()) # Expected output: \"Base fare with extra charge\""
" def fare(self):\n",
" return f\"{super().fare()} with extra charge: {self.bus_fare}\"\n",
"\n",
"school_bus = Bus(10)\n",
"print(school_bus.fare()) # Output: \"Base fare with extra charge: 10\"\n",
"# Output: \"Base fare with extra charge\"\n"
]
},
{
Expand All @@ -153,7 +171,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 24,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -188,19 +206,18 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 30,
"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",
"class Vehicle:\n",
" def __init__(self, name, mileage, capacity):\n",
" self.name = name\n",
Expand All @@ -211,11 +228,14 @@
" return self.capacity * 100\n",
"\n",
"class Bus(Vehicle):\n",
" # Hint: Override fare method to include maintenance charge\n",
" pass\n",
" def fare(self):\n",
" base_fare = super().fare() #super().fare() gets the base fare (capacity * 100) from Vehicle\n",
" maintenance_charge = 0.10 * base_fare # maintenannce charge is defined\n",
" return base_fare + maintenance_charge # base fare calculated\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()) \n",
"# Expected output: Total Bus fare is: (calculated amount)"
]
},
{
Expand All @@ -228,7 +248,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 31,
"metadata": {},
"outputs": [
{
Expand All @@ -255,7 +275,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 32,
"metadata": {},
"outputs": [
{
Expand Down