Skip to content

Week_2_Day_2 | LAB | Object-oriented ProgrammingAdd files via upload #82

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
364 changes: 364 additions & 0 deletions Week_2_Day_2 | LAB | Object-oriented Programming.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,364 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# LAB | Object-Oriented Programming (OOP) in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Overview\n",
"This exercise notebook will help you practice Object-Oriented Programming concepts in Python. You will create classes, instantiate objects, and use inheritance to build more complex structures.\n",
"\n",
"## Instructions\n",
"- Complete each exercise by writing the appropriate code in the provided space.\n",
"- Test your code to ensure it works as expected.\n",
"- Use the hints provided if you get stuck."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 1: Create a Class with Instance Attributes\n",
"Write a Python program to create a `Vehicle` class with `max_speed` and `mileage` instance attributes."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"214 0\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",
"\n",
"# Example instantiation\n",
"model_GTV12 = Vehicle(214,0) # Create an instance of Vehicle\n",
"\n",
"# Print attributes\n",
"print(model_GTV12.max_speed, model_GTV12.mileage) # Expected output: (value of max_speed, value of mileage)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 2: Create a Vehicle Class Without Any Variables and Methods\n",
"Create a `Vehicle` class without any variables or methods."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class '__main__.Vehicle'>\n"
]
}
],
"source": [
"class Vehicle:\n",
" pass\n",
"\n",
"my_vehicle = Vehicle()\n",
"\n",
"print(type(my_vehicle))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 3: Create a Child Class Bus\n",
"Create a child class `Bus` that will inherit all of the variables and methods of the `Vehicle` class."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"class Bus(Vehicle):\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 4: Class Inheritance with Method Overriding\n",
"Create a `Bus` class that inherits from the `Vehicle` class. Override the `fare()` method to include an extra charge for maintenance."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Base fare: 5\n",
"The maintenance costs are: 250 ..... and you're going to pay for it all 😈\n",
"Total fare: 255\n"
]
},
{
"data": {
"text/plain": [
"\"Please note that I don't understand the coinciding of a bus fare (usually paid by the customer) and maintenance fare (usually paid by the buscompany, \\nif they don't have technisions themselves). \\n\\nIn all other cases, maintenance fares should be in the overhead costs, thus in the busfare. But maybe I don't interpret the exercise well\""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Vehicle:\n",
" def fare (self, base_fare):\n",
" return base_fare\n",
"\n",
"class Bus(Vehicle):\n",
" def __init__(self, hourly_rate = 50):\n",
" self.hourly_rate = hourly_rate\n",
" \n",
" def calculate_maintenance_costs(self, hours):\n",
" return hours * self.hourly_rate\n",
"\n",
" def fare (self, base_fare, hours):\n",
" maintenance_charge = self.calculate_maintenance_costs(hours)\n",
" return base_fare + maintenance_charge\n",
" \n",
"base_fare = 5\n",
"hours_worked = 5 \n",
" \n",
"bus = Bus() \n",
" \n",
"total_fare = bus.fare(base_fare, hours_worked)\n",
"\n",
"\n",
"\n",
"print(\"Base fare: \", base_fare)\n",
"print(f\"The maintenance costs are: {bus.calculate_maintenance_costs(hours_worked)} ..... and you're going to pay for it all 😈\")\n",
"print(\"Total fare: \", total_fare) \n",
"\n",
"'''Please note that I don't understand the coinciding of a bus fare (usually paid by the customer) and maintenance fare (usually paid by the buscompany, \n",
"if they don't have technisions themselves). \n",
"\n",
"In all other cases, maintenance fares should be in the overhead costs, thus in the busfare. But maybe I don't interpret the exercise well'''"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 5: Define a Class Attribute\n",
"Define a property that must have the same value for every class instance (object). Set a default value for `color`."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The colour of the bus is: green\n"
]
}
],
"source": [
"class Bus(Vehicle):\n",
" def __init__ (self, colour):\n",
" self.colour = colour\n",
" \n",
"bus_colour = \"green\"\n",
"\n",
"print(f\"The colour of the bus is: {bus_colour}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 6: Class Inheritance with Default Fare Calculation\n",
"Create a `Bus` child class that inherits from the `Vehicle` class. The default fare charge of any vehicle is `seating capacity * 100`. If the vehicle is a bus instance, add an extra 10% on the full fare as a maintenance charge."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bus name: Super bus\n",
"Total fare (including maintenance fare): 11000.00\n"
]
}
],
"source": [
"class Vehicle:\n",
"\n",
" def __init__(self, name, max_speed, mileage):\n",
" self.name = name\n",
" self.max_speed= max_speed\n",
" self.mileage = mileage\n",
"\n",
"def fare(self):\n",
" return \"Base fare not implemented\"\n",
"\n",
"class Bus(Vehicle):\n",
" def __init__(self, name, max_speed, mileage, capacity):\n",
" super().__init__(name, max_speed, mileage)\n",
" self.capacity = capacity\n",
"\n",
" def calculate_fare(self):\n",
" base_fare = self.capacity * 100\n",
" maintenance_charge = base_fare * 0.1\n",
" return base_fare + maintenance_charge\n",
" \n",
"bus = Bus(\"Super bus\", 250, 12000, 100)\n",
"\n",
"print(f\"Bus name: {bus.name}\")\n",
"print(f\"Total fare (including maintenance fare): {bus.calculate_fare():.2f}\")\n",
"\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 7: Check Type of an Object\n",
"Write a program to determine which class a given object belongs to."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'type'>\n"
]
},
{
"data": {
"text/plain": [
"\"I don't get this __main__ part. But it might be, because the downloaden link with the homework was already completely done and I cleared all fields before starting the challenge. \\nThat might affect all output hereabove, actually\""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"super_bus = Bus\n",
"print(type(super_bus))\n",
"# Expected output: <class '__main__.Bus'>\n",
"'''I don't get this __main__ part. But it might be, because the downloaden link with the homework was already completely done and I cleared all fields before starting the challenge. \n",
"That might affect all output hereabove, actually'''"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 8: Check Instance of Class \n",
"Determine if `school_bus` is also an instance of the `Vehicle` class.\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"print(isinstance(super_bus, Vehicle))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise Completion \n",
"Once you have completed all exercises:\n",
"- Review your solutions.\n",
"- Ensure your code is well-documented with comments explaining your logic.\n",
"- Save your notebook for submission or further review.\n",
"\n",
"Happy coding! Enjoy exploring Object-Oriented Programming with Python!\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}