Skip to content
Book 4: Practical Programming

Book 4 · Chapter P4.8

Object-oriented programming

§20.1In codeFree sample chapterBanker · ≈ 38 m · →B4·8

This chapter is free to read here.

Read it in full on cswithzak.com — every question with its mark scheme, every program ready to run. The free practice for each section is below, with the short code the book prints beside it.

Read it free

§20.1 Programming paradigms · Object-oriented programming · Declarative programming

Short code B4·8·1

Objects you have already met

Short code B4·8·10

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Class, object, attribute, method

Short code B4·8·11

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

The constructor

Short code B4·8·12

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Encapsulation: getters and setters

Short code B4·8·13

class Thermostat:
    # Room : STRING
    # Target : INTEGER
    def __init__(self, PRoom):
        self.__Room = PRoom
        self.__Target = 20
    def GetRoom(self):
        return self.__Room
    def GetTarget(self):
        return self.__Target
    def SetTarget(self, Degrees):
        if Degrees >= 10 and Degrees <= 30:
            self.__Target = Degrees

Hall = Thermostat("Hall")
for Wanted in [24, 45, 10]:
    Hall.SetTarget(Wanted)
    print(Hall.GetTarget())
The setter refuses 45, so Target keeps 24; 10 is on the boundary and is accepted.

Output

24
24
10
Run and checked before printing Run it in the lab

Methods that change an object

Short code B4·8·14

class Batter:
    # Name : STRING
    # Runs : INTEGER
    # Balls : INTEGER
    def __init__(self, PName):
        self.__Name = PName
        self.__Runs = 0
        self.__Balls = 0
    def FaceBall(self, Scored):
        self.__Balls = self.__Balls + 1
        self.__Runs = self.__Runs + Scored
    def StrikeRate(self):
        if self.__Balls == 0:
            return 0
        return self.__Runs * 100 // self.__Balls
    def Summary(self):
        return (self.__Name + " "
                + str(self.__Runs) + " off "
                + str(self.__Balls))

Opener = Batter("Ayesha")
for Scored in [4, 0, 1, 6, 2, 1]:
    Opener.FaceBall(Scored)
print(Opener.Summary())
print("Strike rate", Opener.StrikeRate())
FaceBall adds to the attributes; StrikeRate uses whole-number division in each language.

Output

Ayesha 14 off 6
Strike rate 233
Run and checked before printing Run it in the lab

Creating and storing objects

Short code B4·8·15

class Parcel:
    # ParcelID : STRING
    # Weight : INTEGER (grams)
    def __init__(self, PParcelID, PWeight):
        self.__ParcelID = PParcelID
        self.__Weight = PWeight
    def GetParcelID(self):
        return self.__ParcelID
    def GetWeight(self):
        return self.__Weight

Items = [None] * 4
Items[0] = Parcel("P1", 800)
Items[1] = Parcel("P2", 1250)
Items[2] = Parcel("P3", 2400)
Items[3] = Parcel("P4", 950)
Top = 0
for Index in range(1, 4):
    Mass = Items[Index].GetWeight()
    if Mass > Items[Top].GetWeight():
        Top = Index
print("Heaviest:", Items[Top].GetParcelID())
An array of four Parcel objects. The loop reads each weight through the getter, never directly.

Output

Heaviest: P3
Run and checked before printing Run it in the lab

Designing classes from a scenario

Short code B4·8·16

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Inheritance

Short code B4·8·17

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Polymorphism and overriding

Short code B4·8·18

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Containment

Short code B4·8·19

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

From task to evidence on Paper 4

Short code B4·8·20

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Workbook 4: Object-oriented programming

Short code W4·8 · Practical Programming Task Book

The write-in workbook for this chapter — drills, exam-style practice, trace tables, fix-the-mistake and mark-it-yourself pages. Its full mark schemes and an interactive version arrive here with the chapter.

Starter files

Short code W4·8·K

Data files and skeleton code in Python, VB.NET, Java for this chapter's tasks arrive here with the chapter.

Ask Z about Object-oriented programming
Enroll nowOnline classes