Intro to Object Oriented Programming (OOP) #
This lab introduces object oriented programming (OOP).
Syllabus Topics [SL] #
- B3.1.2 Construct a design of classes, their methods and behaviour.
- B3.1.4 Construct code to define classes and instantiate objects.
Key Vocabulary #
| Word | Definition |
|---|---|
| Object Oriented Programming | A style of programming that creates objects using classes, allowing for bundling of variables and functions. |
| Class | A template or blueprint for creating objects. The class defines the attributes and methods. |
| Constructor | A method that creates a new object of a class and initializes the values of the attributes. |
| Object | A specific instance of a class that has its own instance variables/attributes. |
| Instantiation | The process of creating a new instance of a class (aka a new object), by calling its constructor. |
| Methods | Functions that belong to a class. |
| Attributes | Variables that belong to an object. |
| Private Attribute | Accessible only within its own class. |
| Public Attribute | Accessible to any code in program. |
| Accessor methods | Aka Getters. Methods that are used to return the private attributes. |
| Mutator methods | Aka Setters. Methods that are used to update the private attributes. |
[0] Set up #
๐ป Go to your
dpcs folder and create a new folder for this unit.
cd ~/desktop/dpcs/
mkdir unit04_oop
cd unit04_oop
๐ป Clone your repo. This will copy it onto your computer.
Be sure to replace yourgithubusername with your actual username.
git clone https://github.com/isf-dp-cs/lab_oop_intro_yourgithubusername
cd lab_oop_intro_yourgithubusername
๐ป Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.
poetry shell
๐พ ๐ฌ Exiting the poetry shellWhen you want to exit the shell, you can type
exitor^D
[1] UML & Class Definition #
This is a UML (unified modelling language) class diagram. It is used to represent a class, its attributes, and methods.
classDiagram
class Song {
- title: string
- artist: string
- album: string
- popularity: integer
- isExplicit: boolean
- genre: string
- length: float
- energy: float
- loudness: float
- valence: float
+ _ _ init_ _(title, artist, album, popularity, isExplicit, genre, length, energy, loudness, valence)
+ accessor methods ()
+ _ _ str_ _()
}
- First row is the name of the class
- Second row is the attributes
- Third row is the methods
- The
+or-indicates if the attribute/method is public or private
๐ Here is the Python representation of the Song class.
__init__- is the constructorself.__title- is one of the the private attributesget_title()- is one of the public accessor methods
class Song:
def __init__(self, title, artist, album, popularity, isExplicit, genre, length, energy, loudness, valence):
self.__title = title
self.__artist = artist
self.__album = album
self.__popularity = popularity
self.__isExplicit = isExplicit
self.__genre = genre
self.__length = length
self.__energy = energy
self.__loudness = loudness
self.__valence = valence
def get_title(self):
return self.__title
# all other accessor methods
๐ป Take a look at the Song class and test its methods at the bottom of the file: python song.py
s = Song('Someone To You', 'BANNERS', 'Where The Shadow Ends', 79, 0, 'indie', 219801, 0.903, -5.497, 0.652)
print(s.get_title())
length = s.get_length()
print(length)
print(s.__title) # why does this cause an error?
[2] Query Songs #
In query.py, there are six functions to query the list of Song objects.
๐ป Construct code for each function. Read the docstring to learn its functionality. After completed each function, test it at the bottom of the file to ensure it works as expected.
getAllGenre(song_list)getRandomSongGenre(song_list, target_genre)getSongByTitle(song_list, target_title)- use linear searchsortPopularity(song_list)- use bubble sortsortValence(song_list)- use selection sortgetSongByPopularity(song_list, target_popularity)- use binary search
๐พ ๐ฌA typical Paper 2 question is to sort or search using objects. It requires you to compare using the accessor methods.
[3] Create your Own Class #
โ๏ธ Sketch the UML for a class of your choosing. Try to model something in real life. It must have:
- 3 private attributes
- all assessors
- all mutators
๐ป Create a new file and construct code the class.
[4] Deliverables #
โกโจ Once you finish the lab, be sure to complete these two steps:๐ Update Syllabus Checklist: Go to your Syllabus Content Checklist in your Google Drive and update it accordingly.
๐ป Push your work to Github
- git status
- git add -A
- git status
- git commit -m “describe your code here”
- git push
- remote