In this lab, we'll compare the procedural paradigm (which is how you have been programming so far), with the object-oriented programming paradigm. Python itself is an object oriented programming language, and almost everything in Python is an 'object'.
Ensure that you are signed into your GitHub account
Join the GitHub Classroom and accept this assignment: Project 9 - Contacts (OOP)
Clone the repository to your computer using GitHub Desktop or open it online using Codespaces
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which can contain both data and methods.
OOP models real-world entities as software objects that have certain attributes and can perform various functions.
Think of each object as a mini-program: it has information it knows (like a person’s name) and things it can do (like say hello).
Class: A blueprint (or template) for creating objects.
Object: An instance of a class. If a class is a blueprint, an object is the actual thing built from that blueprint.
Attribute: These are like the properties or details about the object.
Method: These are like actions you can do with the object.
Procedural Programming is a programming paradigm that follows a series of steps and procedures to execute a task.
The program is built from functions which are reposnible for a specific task each
Data may be stored globally or passed around from one function to another
The program executes sequentially
Object-Oriented Programming (OOP), on the other hand, focuses on creating objects.
These objects model real-world scenarious more intuitively
Each object has its own data and functions
In this procedural approach, the state of each bulb is managed using a dictionary where each key corresponds to a specific bulb and its state.
Each function depends on external data (the light_bulbs dictionary) to perform its task.
In this approach, each instance of the LightBulb class (bulb1 and bulb2) represents a separate light bulb with its own attributes.
The class encapsulates all properties and behaviors, making the LightBulb class a modular component that can be used as needed across different parts of a program or in different programs without changes.
Classes and objects are the two main aspects of object-oriented programming.
Look at the following illustrations to see the difference between class and objects:
So, a class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the variables and functions from the class.
Open lab1.py
Read the code provided and try to understand how the Car class works
Complete the following activities:
Add another property to the Car class called "odometer". This property should be initialised to 0.
Create two Car objects. One should be a red Toyota and the other a blue Ford.
Start the engine of the red Toyota.
Create a method called "drive" that takes a distance as a parameter. The car can only be driven if the engine is on.
Attempt to drive both cars 100Km.
Print the brand, odometer and colour of both cars.
Commit and push your code