ISF DP Computer Science

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 #

WordDefinition
Object Oriented ProgrammingA style of programming that creates objects using classes, allowing for bundling of variables and functions.
ClassA template or blueprint for creating objects. The class defines the attributes and methods.
ConstructorA method that creates a new object of a class and initializes the values of the attributes.
ObjectA specific instance of a class that has its own instance variables/attributes.
InstantiationThe process of creating a new instance of a class (aka a new object), by calling its constructor.
MethodsFunctions that belong to a class.
AttributesVariables that belong to an object.
Private AttributeAccessible only within its own class.
Public AttributeAccessible to any code in program.
Accessor methodsAka Getters. Methods that are used to return the private attributes.
Mutator methodsAka 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
๐Ÿ’ป In the Terminal, type the following command to open the lab folder.
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 shell

When you want to exit the shell, you can type exit or ^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 constructor
  • self.__title - is one of the the private attributes
  • get_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 search
  • sortPopularity(song_list) - use bubble sort
  • sortValence(song_list) - use selection sort
  • getSongByPopularity(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