Book 4 · Chapter P4.8
Object-oriented programming
B4·8This 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.
For the whole chapter
- Topic pageFurther Programming
B4·8·T - QuizFurther Programming
B4·8·Q1 - Quiz9618 Pseudocode Guide — mastery
B4·8·Q2 - LabProgramming tasks (OOP, auto-marked)
B4·8·X1 - LabOOP class designer
B4·8·X2 - LabDeclarative Lab (Prolog)
B4·8·X3 - LabTranslate to Python / VB / Java
B4·8·X4
§20.1 Programming paradigms · Object-oriented programming · Declarative programming
Short code B4·8·1
- FlashcardsFlashcards §20.1: Programming paradigms
B4·8·F1 - FlashcardsFlashcards §20.1: OOP concepts
B4·8·F2 - FlashcardsFlashcards §20.1: 9618 pseudocode — OOP, files & exceptions syntax
B4·8·F3 - Past-paper questionsPast-paper questions §20.1 (programming-paradigms)
- Past-paper questionsPast-paper questions §20.1 (object-oriented-programming)
- Past-paper questionsPast-paper questions §20.1 (declarative-programming)
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())Target keeps 24; 10 is on the boundary and is accepted.Output
24 24 10
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
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())Parcel objects. The loop reads each weight through the getter, never directly.Output
Heaviest: P3
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.


