Further programming: paradigms, OOP, files and exceptions – A Level Computer Science (9618)

13 questions · Clear filters

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
1
9618/41/M/J/26 · Question 3May/June 2026Paper 4121 marks
Loading question…
Question text

3
A prototype for a game is designed using Object‑Oriented Programming (OOP).
The game will allow a character to move along a path (for example a road or sidewalk).
The prototype requires the classes setting up and the path creating ready for the character to
be implemented.
(a) Each space in the path can contain an object. The path and objects are created as classes.
The class TheObject stores the data about the objects. The attributes are public.
TheObject
ObjectType : String
stores the type of object, either "Money" or "Experience"
Amount : Integer
stores the amount of the object, for example 200
Constructor()
initialises the attributes to the parameter values
The class Path stores the data about the objects. The attributes are public.
Path
BoardArray[0:99] : TheObject
an array of 100 objects of type TheObject
NumberSpaces : Integer
stores the number of spaces in the path
Constructor()
initialises NumberSpaces to the parameter value.
Initialises all 100 array objects to an empty object of type
TheObject. An empty object has the values
ObjectType = "" and Amount = –1
GetNumberSpaces()
returns NumberSpaces
InitialiseData()
takes an array of objects of type TheObject as a
parameter and stores them in BoardArray
ReturnPath()
returns a string created using data from each object
in BoardArray
GetPathObject()
takes a number as a parameter and returns the object in
that index in BoardArray
Write program code to declare the class TheObject and its constructor.
(i)
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question3_J26.
Copy and paste the program code into 3(a)(i) in the evidence document.
[4]
© Cambridge University Press & Assessment 2026
Write program code to declare the class Path, its constructor and
(ii)
method GetNumberSpaces()
Use your programming language appropriate constructor.
Do not declare the other methods.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into 3(a)(ii) in the evidence document.
[4]
The method InitialiseData() takes an array of type TheObject as a parameter.
(iii)
The method stores each element from the parameter array into the array BoardArray
in the same order.
If there are more objects in the parameter array than in NumberSpaces, the method
does not store the data and returns FALSE. Otherwise, it returns TRUE
Write program code for InitialiseData()
Save your program.
Copy and paste the program code into 3(a)(iii) in the evidence document.
[3]
The method ReturnPath() creates and returns a string containing the type and
(iv)
amount of each object that is not a null object in BoardArray, in the format:
<ObjectType>-<Amount>
For example, an array with two not null objects produces the string:
Money-100 Experience-50
You can assume that there will always be an object that is not null in BoardArray
Write program code for ReturnPath()
Save your program.
Copy and paste the program code into 3(a)(iv) in the evidence document.
[2]
© Cambridge University Press & Assessment 2026
The method GetPathObject() takes an integer index as a parameter and returns the
(v)
object at the parameter index in BoardArray
Write program code for GetPathObject()
Save your program.
Copy and paste the program code into 3(a)(v) in the evidence document.
[1]
(b) (i)
Write program code for the main program to:
•
create a new array, ArrayOfObjects, of type TheObject with five spaces
•
store the following objects in each space in ArrayOfObjects in the order given:
money with amount 50
experience with amount 100
money with amount 200
money with amount 20
experience with amount 30
•
create a new object, Path1, of type Path with five spaces
•
call InitialiseData() for Path1 with ArrayOfObjects
•
call ReturnPath() for Path1 and output the return value.
Save your program.
Copy and paste the program code into 3(b)(i)

2
9618/42/M/J/26 · Question 2May/June 2026Paper 4230 marks
Loading question…
Question text

2
A program stores data about avatars in a program using Object‑Oriented Programming (OOP).
An avatar is a character that a user can control in a game.
The class Avatar stores the data about avatars.
Avatar
TheName : String
stores the name of the avatar
Glasses : Boolean
stores TRUE if the avatar wears glasses
stores FALSE if the avatar does not wear glasses
Hair : String
stores the colour of the avatar's hair
Age : Integer
stores the age of the avatar
Constructor()
initialises all attributes to its parameter values
GetName()
returns the name of the avatar
GetAge()
returns the age of the avatar
MeetCriteria()
takes the glasses choice and hair choice as parameters
and returns whether the avatar matches the choices
Write program code to declare the class Avatar and its constructor. All attributes must
(a) (i)
be private.
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question2_J26.
Copy and paste the program code into 2(a)(i) in the evidence document.
[4]
The get methods GetName() and GetAge() each return the relevant attribute.
(ii)
Write program code for GetName() and GetAge()
Save your program.
Copy and paste the program code into 2(a)(ii) in the evidence document.
[3]
© Cambridge University Press & Assessment 2026
The method MeetCritera() takes two parameters:
(iii)
•
a Boolean value to identify whether the avatar wears glasses
•
a string value to identify the hair colour.
The method checks whether this avatar’s attributes match both of the parameters.
Boolean TRUE is returned when both of the attributes match the parameters, otherwise
Boolean FALSE is returned.
Write program code for MeetCriteria()
Save your program.
Copy and paste the program code into 2(a)(iii) in the evidence document.
[2]
The text file AvatarFile.txt stores data for 30 avatars. Each avatar is on a new line
(b) (i)
in the file. The data is stored in the order: name, glasses, hair colour, age. Each value is
separated by a space.
For example, the first line in the file is:
Kit True blonde 21
The name of the avatar is Kit, the avatar wears glasses, the avatar has blonde hair, the
avatar is 21 years old.
The procedure LoadAvatars() reads the data from the text file into the program. The
function creates a new object for each avatar in the file and stores the avatar objects in
a global array with the identifier AvailableAvatars
The procedure uses exception handling when opening and reading from the file.
Write program code for LoadAvatars()
Save your program.
Copy and paste the program code into 2(b)(i) in the evidence document.
[7]
Write program code for the main program to call LoadAvatars()
(ii)
Save your program.
Copy and paste the program code into 2(b)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
The procedure EnterChoices() prompts the user to enter whether they want an
(c) (i)
avatar that wears glasses and the hair colour they want.
The procedure takes these two values as input until they are valid:
•
wearing glasses can only be yes or no
•
the hair colour can be black, blonde, brown, pink, red or green.
The procedure calls the method MeetCriteria() for each avatar in
AvailableAvatars and stores the avatars that match the criteria in a global array.
Write program code for EnterChoices()
Save your program.
Copy and paste the program code into 2(c)(i) in the evidence document.
[6]
(ii)
Write program code to extend the main program to:
•
call EnterChoices()
•
output the quantity of avatars that match the criteria
•
output the name and age of each avatar that matches the criteria.
All outputs must include an appropriate message.
Save your program.
Copy and paste the program code into 2(c)(ii) in the evidence document.
[5]
© Cambridge University Press & Assessment 2026
(d) (i)
Test your program with the following data as input:

3
9618/43/M/J/26 · Question 2May/June 2026Paper 4329 marks
Loading question…
Question text

2
A prototype for a game is being developed using Object‑Oriented Programming (OOP).
The game has a set of characters that have data stored about their characteristics, for example
if the character can fly or not fly. A user has to select their requirements, for example a character
that can fly, and the program will output the characters that meet the user’s requirements.
The class Character stores the data about the characters. The attributes are public.
Character
CharacterName : String
stores the character’s name
Colour : String
stores the colour of the character, for
example "Green"
SuperStrength : String
stores whether the character has super strength
("Yes") or not ("No")
Fly : String
stores whether the character can fly ("Yes") or
not ("No")
Invisible : String
stores whether the character can become
invisible ("Yes") or not ("No")
Constructor()
initialises the attributes to its parameter values
GetCharacterName()
returns CharacterName
GetColour()
returns Colour
GetSuperStrength()
returns SuperStrength
GetFly()
returns Fly
GetInvisible()
returns Invisible
Write program code to declare the class Character and its constructor.
(a) (i)
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question2_J26.
Copy and paste the program code into 2(a)(i) in the evidence document.
[4]
© Cambridge University Press & Assessment 2026
The get methods GetCharacterName(), GetColour(), GetSuperStrength(),
(ii)
GetFly() and GetInvisible() each return the appropriate attribute.
Write program code for GetCharacterName(), GetColour(),
GetSuperStrength(), GetFly() and GetInvisible()
Save your program.
Copy and paste the program code into 2(a)(ii) in the evidence document.
[3]
(b) The text file Characters.txt stores data about 15 characters. Each character is on a new
line in the file. The data is stored in the order: Character name, colour, super strength, fly,
invisible. Each value is separated by a comma.
For example, the first line in the file is:
Jet,Black,Yes,No,No
The name of the character is Jet. The colour of the character is Black. The character has
super strength. The character cannot fly. The character cannot become invisible.
The function ReadData():
•
opens the file Characters.txt and reads in the data
•
creates a new Character object for each character in the file
•
stores each Character object in a local array
•
returns the populated array.
The function uses exception handling when opening and reading from the file.
Write program code for ReadData()
Save your program.
Copy and paste the program code into 2(b) in the evidence document.
[7]
© Cambridge University Press & Assessment 2026
(c) The procedure OutputCharacters() takes an array of Character objects as a
parameter. The procedure outputs the data for each character on a new line, in the format:
Name: <Character name> Colour: <Colour> Super strength: <Super
strength> Fly: <Fly> Invisible: <Invisible>
For example:
Name: Jet Colour: Black Super strength: Yes Fly: No Invisible: No
Write program code for OutputCharacters()
Save your program.
Copy and paste the program code into 2(c) in the evidence document.
[3]
The main program calls ReadData() and then OutputCharacters() with the
(d) (i)
returned array.
Write program code for the main program.
Save your program.
Copy and paste the program code into 2(d)(i) in the evidence document.
[2]
(ii)
Test your program
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 2(d)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
(e) The main program needs to give the user two choices to limit the characters that they can
choose from. For example, they could choose to only see characters that are green and that
can fly.
(i)
The main program needs extending to:
•
prompt the user to enter two criteria and take both as input.

Advertisement
4
9618/41/M/J/25 · Question 3May/June 2025Paper 4130 marks
Loading question…
Question text

3
A program stores data in a binary tree that is designed using Object‑Oriented Programming
(OOP).
The tree stores data in ascending numerical order, for example:
30
20
45
1
21
33
63
The class Node stores data about the nodes.
Node
NodeData : Integer
stores the node’s integer data
LeftNode : Node
stores the node that is stored to the left of the current
node, or a null value if there is no node to the left
RightNode : Node
stores the node that is stored to the right of the current
node, or a null value if there is no node to the right
Constructor()
initialises NodeData to its parameter value; initialises
LeftNode and RightNode to a null value
GetLeft()
returns LeftNode
GetRight()
returns RightNode
GetData()
returns NodeData
SetLeft()
takes an object of type Node as a parameter and stores
it in LeftNode
SetRight()
takes an object of type Node as a parameter and stores
it in RightNode
Write program code to declare the class Node and its constructor.
(a) (i)
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question3_J25.
Copy and paste the program code into part 3(a)(i) in the evidence document.
[4]
(ii)
Write program code for the three get methods.
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
[3]
The method SetLeft() takes an object of type Node as a parameter. The method
(iii)
stores the parameter in the attribute LeftNode
The method SetRight() takes an object of type Node as a parameter. The method
stores the parameter in the attribute RightNode
Write program code for SetLeft() and SetRight()
Save your program.
Copy and paste the program code into part 3(a)(iii) in the evidence document.
[3]
(b) Write the main program to declare five objects of type Node :
•
Node 1 with the data 10
•
Node 2 with the data 20
•
Node 3 with the data 5
•
Node 4 with the data 15
•
Node 5 with the data 7
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[2]
(c) The class Tree stores the tree.
Tree
FirstNode : Node
stores the root node in the tree
Constructor()
initialises FirstNode to its parameter value
GetRootNode()
returns the node stored in FirstNode
Insert()
stores its parameter node in the correct position in the
tree
Write program code to declare the class Tree and its constructor.
(i)
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[2]
Write program code for GetRootNode()
(ii)
Save your program.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
[1]
The method Insert() takes a node as a parameter and then searches the tree to find
(iii)
the position to insert the new node by:
•
checking whether the node’s data is less than or greater than the root node’s data
•
moving to the left node if the data is less than the root node’s data
•
moving to the right node if the data is greater than or equal to the root node’s data
•
repeating until the final position of the node is found and stores the node in that
position.
Write program code for Insert()
Save your program.
Copy and paste the program code into part 3(c)(iii) in the evidence document.
[6]
(d) The recursive procedure OutputInOrder() outputs the data stored in the binary tree in
ascending numerical order.
The procedure takes a Node object as a parameter and then:
•
checks if the left node is null. If there is a left node, the function calls itself with the left
node
•
outputs the data of the current node
•
checks if the right node is null. If there is a right node, the function calls itself with the
right node.
Write program code for OutputInOrder()
Save your program.
Copy an

5
9618/42/M/J/25 · Question 3May/June 2025Paper 4226 marks
Loading question…
Question text

3
A program stores data about animals using Object‑Oriented Programming (OOP).
The class Animal stores the data about animals.
Animal
Name : String
stores the name of the animal
Sound : String
stores the sound the animal makes
Size : Integer
stores the size of the animal as an integer between 1 (smallest)
and 10 (largest)
Intelligence : Integer
stores the intelligence of the animal as an integer between 1 (least)
and 10 (most)
Constructor()
initialises all attributes to its parameter values
Description()
returns a string message that contains the data from the attributes
Write program code to declare the class Animal and its constructor.
(a) (i)
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question3_J25.
Copy and paste the program code into part 3(a)(i) in the evidence document.
[4]
The method Description() creates and returns a string of the animal’s data in the
(ii)
format:
"The animal's name is " <Name> ", it makes a " <Sound> ", its size
is " <Size> " and its intelligence level is " <Intelligence>
For example:
The animal's name is Teddy, it makes a Bark, its size is 4 and its
intelligence level is 6
Write program code for Description()
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
[3]
(b) The class Parrot inherits from the class Animal
Parrot inherits the attributes from Animal and overrides the Description() method. The
additional attributes and methods in the class Parrot are:
Parrot
WingSpan : Integer
the width of the parrot’s wings to the nearest cm
NumberWords : Integer
the number of words the parrot can speak
Constructor()
calls the parent constructor using its parameter values;
initialises WingSpan and NumberWords to its parameter values
ChangeNumberWords()
takes an integer parameter and adds this to the number currently
in NumberWords
Write program code to declare the class Parrot, its constructor and the method
(i)
ChangeNumberWords()
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
[4]
The method Description() in the class Parrot creates and returns a string of the
(ii)
animal’s data in the format:
"The animal's name is " <Name> ", it makes a " <Sound> ", its size
is " <Size> " and its intelligence level is " <Intelligence> ". It
has a wingspan of " <WingSpan> "cm and can say " <NumberWords> "
words."
For example:
The animal's name is Chewie, it makes a Squawk, its size is 1 and
its intelligence level is 10. It has a wingspan of 30cm and can
say 29 words.
Write program code for Description()
Save your program.
Copy and paste the program code into part 3(b)(ii) in the evidence document.
[2]
(c) The class Wolf inherits from the class Animal
Wolf inherits the attributes from Animal and overrides the Description() method. The
additional attributes and methods in the class Wolf are:
Wolf
TerritorySize : Integer
stores the size of the area where the wolf lives, to the nearest
square mile
Constructor()
calls the parent constructor using its parameter values;
initialises TerritorySize to its parameter value
SetTerritorySize()
takes an integer parameter and adds this to the number currently
in TerritorySize
Write program code to declare the class Wolf, its constructor and the method
(i)
SetTerritorySize()
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[4]
The method Description() in the class Wolf creates and returns a string of the
(ii)
animal’s data in the format:
"The animal's name is " <Name> ", it makes a " <Sound> ", its size
is " <Size

6
9618/43/M/J/25 · Question 3May/June 2025Paper 4329 marks
Loading question…
Question text

3
A program stores data in a linked list that is designed using Object‑Oriented Programming (OOP).
The class Node stores data about the nodes.
Node
TheData : Integer
stores the integer data
NextNode : Node
stores the next node in the linked list
Constructor()
initialises TheData to its parameter value, initialises NextNode
to a null value
GetData()
returns TheData
GetNextNode()
returns NextNode
SetNextNode()
takes an object of type Node as a parameter and stores it in
NextNode
Write program code to declare the class Node and its constructor.
(a) (i)
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question3_J25.
Copy and paste the program code into part 3(a)(i) in the evidence document.
[4]
(ii)
Write program code for the two get methods.
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
[3]
The method SetNextNode() takes an object of type Node as a parameter. The method
(iii)
stores the parameter in the attribute NextNode
Write program code for SetNextNode()
Save your program.
Copy and paste the program code into part 3(a)(iii) in the evidence document.
[2]
(b) The class LinkedList stores the linked list.
LinkedList
HeadNode : Node
stores the first node in the linked list
Constructor()
initialises HeadNode to a null value
InsertNode()
creates a new node using its integer parameter. Sets this
node as the new head node and updates NextNode
RemoveNode()
finds the first node that contains its integer parameter and
removes this node from the linked list
Traverse()
concatenates and returns the integer data in each node in
the linked list
Write program code to declare the class LinkedList and its constructor.
(i)
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
[2]
The method InsertNode():
(ii)
•
takes an integer as a parameter
•
creates a new node with the parameter as the integer data
•
uses the new node’s method SetNextNode() to store the current HeadNode as
the next node
•
replaces HeadNode with the current node.
Write program code for InsertNode()
Save your program.
Copy and paste the program code into part 3(b)(ii) in the evidence document.
[4]
The method Traverse() concatenates the integer data from the nodes in the linked
(iii)
list, starting with the node stored in HeadNode. The method returns the final string with
each integer data separated with a space.
Write program code for Traverse()
Save your program.
Copy and paste the program code into part 3(b)(iii) in the evidence document.
[3]
The method RemoveNode() takes an integer parameter to search for and remove from
(iv)
the linked list.
The method first checks if the linked list is empty. If the linked list is empty the method
returns FALSE
If the linked list is not empty, the integer data in HeadNode is compared to the parameter.
If it matches the parameter, HeadNode is changed to store the next node.
If the parameter does not match, the nodes are followed until either:
•
the node with matching integer data is found. This node is removed, the appropriate
nodes updated and TRUE returned
or
•
none of the nodes contain matching integer data to the parameter. No nodes are
removed and FALSE is returned.
Write program code for RemoveNode()
Save your program.
Copy and paste the program code into part 3(b)(iv) in the evidence document.
[6]
The main program creates a new LinkedList object and uses the appropriate method
(c) (i)
to insert five nodes with the following integer data values in the order given:
10
20
30
40
50
The main program then:
•
calls Traverse()
•
removes the node containing the integer data 30 using Remov

7
9618/41/M/J/24 · Question 2May/June 2024Paper 4131 marks
Loading question…
Question text

2
A computer program will store data about trees.
The user can enter their requirements for a tree and a suitable tree will be selected.
The program is written using object‑oriented programming.
The class Tree stores data about the trees.
Tree
TreeName : STRING
stores the name of the tree
HeightGrowth : INTEGER
stores the number of cm the tree will grow each year
MaxHeight : INTEGER
stores the maximum height in cm that the tree will grow
MaxWidth : INTEGER
stores the maximum width in cm that the tree will grow
Evergreen : STRING
stores whether the tree keeps its leaves as "Yes", or
loses its leaves as "No"
Constructor()
initialises TreeName, HeightGrowth, MaxHeight,
MaxWidth and Evergreen to its parameter values
GetTreeName()
returns the name of the tree
GetGrowth()
returns the number of cm the tree will grow each year
GetMaxHeight()
returns the maximum height in cm that the tree will grow
GetMaxWidth()
returns the maximum width in cm that the tree will grow
GetEvergreen()
returns whether the tree keeps its leaves or loses its
leaves
Write program code to declare the class Tree and its constructor.
(a) (i)
Do not declare the other methods.
Use the appropriate constructor for your programming language.
All attributes must be private.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question2_J24.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[4]
The get methods GetTreeName(), GetGrowth(), GetMaxHeight(),
(ii)
GetMaxWidth() and GetEvergreen() each return the relevant attribute.
Write program code for the get methods.
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[3]
(b) The text file Trees.txt stores data about 9 trees.
The data in the file is stored in the format:
Tree name,Height growth each year,Maximum height,Maximum width,Evergreen
For example, the first row of data is:
Beech,30,400,200,No
The tree is a Beech. It can grow 30 cm each year. It has a maximum height of 400 cm.
It has a maximum width of 200 cm. It is not evergreen (it loses its leaves).
The function ReadData():
•
creates an array of type Tree
•
reads the data from the file
•
raises an exception if the file is not found
•
creates a new object of type Tree for each tree in the file
•
appends each object to the array
•
returns the array.
Write program code for ReadData().
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[7]
(c) The procedure PrintTrees() takes a Tree object as a parameter and outputs the tree’s
name, height growth each year, maximum height, maximum width and whether it is evergreen.
The output message changes depending on whether it is evergreen.
If it is evergreen, it is in the format:
TreeName has a maximum height MaxHeight a maximum width MaxWidth and grows
HeightGrowth cm a year. It does not lose its leaves.
If it is not evergreen, it is in the format:
TreeName has a maximum height MaxHeight a maximum width MaxWidth and grows
HeightGrowth cm a year. It loses its leaves each year.
Write program code for PrintTrees().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[4]
(d) The main program calls ReadData(), stores the return value and calls PrintTrees() with
the first object in the returned array.
(i)
Write program code for the main program.
Save your program.
Copy and paste the program code into part 2(d)(i) in the evidence document.
[2]
(ii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 2(d)(ii) in the evidence document.
[1]
(e) The procedure ChooseTree() takes an array of Tree objects as a parameter.
The procedure prompts the user to input their requirements for a tree.
The user needs to enter:
•
the maximum height the tree can be in cm
•
the maximum width the tree can be in cm
•
whether they want the tree to

8
9618/42/M/J/24 · Question 2May/June 2024Paper 4230 marks
Loading question…
Question text

2
A binary tree stores data in ascending order. For example:
15
8
19
3
10
A computer program stores integers in a binary tree in ascending order. The program uses
Object‑Oriented Programming (OOP).
The binary tree is stored as a 1D array of nodes. Each node contains a left pointer, a data value
and a right pointer.
The class Node stores the data about a node.
Node
LeftPointer : INTEGER
stores the index of the node to the left in the binary tree
Data : INTEGER
stores the node’s data
RightPointer : INTEGER
stores the index of the node to the right in the binary tree
Constructor()
initialises Data to its parameter value
initialises LeftPointer and RightPointer to −1
GetLeft()
returns the left pointer
GetRight()
returns the right pointer
GetData()
returns the data value
SetLeft()
assigns the parameter to the left pointer
SetRight()
assigns the parameter to the right pointer
SetData()
assigns the parameter to the data
Write program code to declare the class Node and its constructor.
(a) (i)
Do not declare the other methods.
Use the appropriate constructor for your programming language.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question2_J24.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[4]
The get methods GetLeft(), GetRight()and GetData()each return the relevant
(ii)
attribute.
Write program code for the three get methods.
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[3]
The set methods SetLeft(), SetRight()and SetData()each take a parameter and
(iii)
then store this in the relevant attribute.
Write program code for the three set methods.
Save your program.
Copy and paste the program code into part 2(a)(iii) in the evidence document.
[3]
(b) The class TreeClass stores the data about the binary tree.
TreeClass
Tree[0:19] : Node
an array of 20 elements of type Node
FirstNode : INTEGER
stores the index of the first node in the tree
NumberNodes : INTEGER
stores the quantity of nodes in the tree
Constructor()
initialises FirstNode to −1 and NumberNodes to 0
initialises each element in Tree to a Node object with the
data value of −1
InsertNode()
takes a Node object as a parameter, inserts it in the array
and updates the pointer for its parent node
OutputTree()
outputs the left pointer, data and right pointer of each
node in Tree
Nodes cannot be deleted from this tree.
Write program code to declare the class TreeClass and its constructor.
(i)
Do not declare the other methods.
Use the appropriate constructor for your programming language.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[4]
The method InsertNode()takes a Node object, NewNode, as a parameter and inserts
(ii)
it into the array Tree.
InsertNode()first checks if the tree is empty.
If the tree is empty, InsertNode():
•
stores NewNode in the array Tree at index NumberNodes
•
increments NumberNodes
•
stores 0 in FirstNode.
If the tree is not empty, InsertNode():
•
stores NewNode in the array Tree at index NumberNodes
•
accesses the data in the array Tree at index FirstNode and compares it to the
data in NewNode
•
repeatedly follows the pointers until the correct position for NewNode is found
•
once the position is found, InsertNode()sets the left or right pointer of its parent
node
•
increments NumberNodes.
Write program code for InsertNode().
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[6]
The method OutputTree()outputs the left pointer, the data and the right pointer for
(iii)
each node that has been inserted into the tree. The outputs are in the order they are
saved in the array.
If there are no nodes in the array, the procedure outputs ‘No nodes’.
Write program code for OutputTree().
Save your program.
Copy and paste the progra

Advertisement
9
9618/41/M/J/23 · Question 2May/June 2023Paper 4132 marks
Loading question…
Question text

2
A computer game is being designed that will include different vehicles. A prototype for the game is
being developed using object‑oriented programming.
The class Vehicle stores data about the vehicles. Each vehicle has an identification name,
a maximum speed, a current speed and a horizontal position. The value IncreaseAmount is
added to the current speed each time the vehicle increases its speed.
Vehicle
ID : STRING
stores the identification name for the vehicle
MaxSpeed : INTEGER
stores the maximum speed
CurrentSpeed : INTEGER
stores the current speed
IncreaseAmount : INTEGER
stores the amount CurrentSpeed increases by
HorizontalPosition : INTEGER
stores the horizontal position
Constructor()
initialises ID, MaxSpeed and IncreaseAmount to the
parameter values
initialises both CurrentSpeed and
HorizontalPosition to 0
GetCurrentSpeed()
returns the current speed
GetIncreaseAmount()
returns the increase amount
GetHorizontalPosition()
returns the horizontal position
GetMaxSpeed()
returns the maximum speed
SetCurrentSpeed()
assigns the parameter to the current speed
SetHorizontalPosition()
assigns the parameter to the horizontal position
IncreaseSpeed()
calculates and stores the new speed and horizontal
position of the vehicle
(a) (i)
Write program code to declare the class Vehicle. All attributes must be private.
You only need to declare the class and its constructor. Do not declare any other methods.
Use your programming language’s appropriate constructor.
If you are writing program code in Python, include attribute declarations using comments.
Save your program as Question2_J2023.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[5]
(ii)
Write
program
code
for
the
get
methods
GetCurrentSpeed(),
GetIncreaseAmount(), GetMaxSpeed() and GetHorizontalPosition()
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[3]
(iii)
Write program code for
the
set
methods
and
SetCurrentSpeed()
SetHorizontalPosition()
Save your program.
Copy and paste the program code into part 2(a)(iii) in the evidence document.
[3]
(iv)
The method IncreaseSpeed():
•
adds IncreaseAmount to the current speed
•
adds the updated current speed to the horizontal position.
The current speed of a vehicle cannot exceed its maximum speed.
Write program code for the method IncreaseSpeed()
Save your program.
Copy and paste the program code into part 2(a)(iv) in the evidence document.
[3]
(b) The child class Helicopter inherits from the parent class Vehicle. A helicopter also has a
vertical position and changes the vertical position when it increases speed.
Helicopter
VerticalPosition : INTEGER
stores the vertical position
VerticalChange : INTEGER
stores the amount VerticalPosition changes by
MaxHeight : INTEGER
stores the maximum height the helicopter can reach
Constructor()
takes the ID, maximum speed, increase amount, vertical
change and maximum height as parameters
initialises the vertical position to 0
GetVerticalPosition()
returns the vertical position
IncreaseSpeed()
changes the current speed, horizontal and vertical
position of the helicopter
(i)
Write program code to declare the class Helicopter. You only need to declare the
class and its constructor. You do not need to declare the other methods.
Use your programming language’s appropriate constructor.
All attributes must be private.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[5]
(ii)
The Helicopter method IncreaseSpeed() overrides the method from the parent
class and:
•
adds the amount of vertical change to the vertical position
•
adds IncreaseAmount to the current speed
•
adds the updated current speed to the horizontal position.
The vertical position of a helicopter cannot exceed its maximum height.
The current speed of a helicopter cannot exceed its maximum speed.
Write pro

10
9618/42/M/J/23 · Question 3May/June 2023Paper 4233 marks
Loading question…
Question text

3
A company needs a computer program to store data about its employees.
Part of the program is being written using object‑oriented programming.
The class Employee stores data about the employees. Each employee has an employee number,
a job title and hourly pay rate. The class will also store the amount they are paid each week over a
52‑week year in a 1D array.
Employee
HourlyPay : REAL
stores the amount each employee gets paid each
hour
EmployeeNumber : STRING
stores the employee’s unique number
JobTitle : STRING
stores the employee’s job title
PayYear2022 : ARRAY[0:51] OF REAL
stores the amount the employee has been paid each
week
Constructor()
initialises HourlyPay, EmployeeNumber and
JobTitle from the values passed as parameters
initialises all 52 elements in PayYear2022 to 0.0
GetEmployeeNumber()
returns the employee number
SetPay()
takes the week number and number of hours worked
that week as parameters
calculates and stores the pay for that week in
PayYear2022
returns the total of all the values in PayYear2022
GetTotalPay()
(a) (i)
Write program code to declare the class Employee.
You only need to declare the class and its constructor. Do not declare any other methods.
Use your programming language appropriate constructor.
If you are writing program code in Python, include attribute declarations using comments.
Save your program as Question3_J2023.
Copy and paste the program code into part 3(a)(i) in the evidence document.
[5]
(ii)
The method GetEmployeeNumber() returns the employee number.
Write program code for the method GetEmployeeNumber().
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
[2]
(iii)
The method SetPay():
•
takes a week number and the number of hours worked that week as parameters
•
calculates the pay for that week by multiplying the hourly pay by the number of
hours worked that week
•
stores the calculated pay in the appropriate index for that week in PayYear2022.
Write program code for the method SetPay().
Save your program.
Copy and paste the program code into part 3(a)(iii) in the evidence document.
[3]
(iv)
The method GetTotalPay() returns the total of all the values in PayYear2022.
Write program code for the method GetTotalPay().
Save your program.
Copy and paste the program code into part 3(a)(iv) in the evidence document.
[2]
(b) The child class Manager inherits from the parent class Employee.
A manager gets a bonus. This bonus value is a percentage, for example 10.0%.
When calculating the pay, the number of hours the manager worked that week is increased
by the bonus value.
Manager
stores the bonus value, for example 10.0 represents a
BonusValue : REAL
10.0% increase
Constructor()
takes bonus value, hourly pay, employee number and job title
as parameters
initialises BonusValue to its parameter value
SetPay()
takes the week number and number of hours worked as
parameters
increases the number of hours worked by the bonus value
calls the SetPay() method from the parent class
(i)
Write program code to declare the class Manager.
You only need to declare the class and its constructor. Do not declare any other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
[4]
(ii)
The Manager method SetPay() overrides the method from the parent class and:
•
takes the week number and number of hours worked as parameters
•
increases the number of hours worked by the bonus value
•
calls SetPay() from the parent class.
Write program code for the method SetPay().
Save your program.
Copy and paste the program code into part 3(b)(ii) in the evidence document.
[3]
(c) The main program has a global 1D array, EmployeeArray, to store data about
eight employees. Each employee is stored as an object of type Employee.
The file Employees.txt sto

11
9618/41/M/J/22 · Question 2May/June 2022Paper 4125 marks
Loading question…
Question text

2
A computer game is being developed using object-oriented programming.
One element of the game is a balloon. This is designed as the class Balloon.
The class has the following attributes and methods.
Balloon
Health : INTEGER
The health of the balloon
Colour : STRING
The colour of the balloon
DefenceItem : STRING
The item the balloon uses to defend itself
Constructor()
Initialises the defence item and colour using the
parameters
Initialises health to 100
ChangeHealth()
Takes the change as a parameter and adds this to the
health
GetDefenceItem()
Returns the defence item of the object
CheckHealth()
If the health is 0 or less, it returns TRUE, otherwise it
returns FALSE
(a) The constructor takes the name of the defence item and the balloon’s colour as parameters
and sets these to the attributes. The health is initialised to 100.
Write program code to declare the class Balloon and its constructor. Do not write any other
methods.
Use your language appropriate constructor.
All attributes should be private. If you are writing in Python include attribute declarations using
comments.
Save your program as Question2_J2022.
Copy and paste the program code into part 2(a) in the evidence document.
[5]
(b) The get method GetDefenceItem() returns the defence item of the object.
Amend your program code to include the get method GetDefenceItem().
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[2]
(c) The object’s method ChangeHealth() takes an integer number as a parameter and adds
this to the health attribute of the object.
Amend your program code to include the method ChangeHealth().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) The object’s method CheckHealth() returns TRUE if the health of the object is 0 or less (no
health remaining) and returns FALSE otherwise (health remaining).
Amend your program code to include the method CheckHealth().
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[2]
(e) Amend the main program to:
•
take as input a defence item and colour from the user
•
create a new balloon with the identifier Balloon1 using the data input.
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[3]
(f)
The function Defend():
•
takes a balloon object as a parameter
•
takes as input the strength of an opponent from the user
•
uses the ChangeHealth() method to subtract the strength input from the object’s
health
•
outputs the defence item of the balloon
•
checks the health of the object and outputs an appropriate message if it has no health
remaining, or if it has health remaining
•
returns the amended balloon object.
Write program code to declare the function Defend().
Save your program.
Copy and paste the program code into part 2(f) in the evidence document.
[8]
(g) (i)
Amend the main program to call the function Defend().
Save your program.
Copy and paste the program code into part 2(g)(i) in the evidence document.
[2]
(ii)
Test your program using the following inputs:
•
balloon defence method "Shield"
•
balloon colour "Red"
•
strength of opponent 50
Take a screenshot to show the output.
Copy and paste the screenshot into part 2(g)(ii) in the evidence document.
[1]

12
9618/42/M/J/22 · Question 3May/June 2022Paper 4227 marks
Loading question…
Question text

3
A programmer is designing a computer game that uses a set of cards.
Each card has a number and a colour. The cards are saved in the text file CardValues.txt
The program has a class named Card. The class has the following attributes and methods.
Card
Number : INTEGER
The number of the card
Colour : STRING
The colour of the card
Constructor()
Takes two values as parameters and sets them to the private
attributes
GetNumber()
Returns the number of the card
Returns the colour of the card
GetColour()
(a) The constructor takes the number and colour of the card as parameters and sets them to the
private attributes.
Write program code to declare the class Card and its constructor. Do not write any other
methods.
Use your programming language appropriate constructor.
All attributes should be private. If you are writing in Python, include attribute declarations
using comments.
Save your program as Question3_J22.
Copy and paste the program code into part 3(a) in the evidence document.
[5]
(b) The two get methods return the associated attribute.
Write program code for the get methods GetNumber() and GetColour().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[3]
(c) The text file CardValues.txt stores the data for 30 cards, in the order: number, colour.
For example, the first card in the text file:
1 is the number
red is the colour.
A 1D array of type Card is declared to store all the cards read in from CardValues.txt
Write the main program to:
•
declare an array of type Card with 30 elements
•
read in the data for the 30 cards from CardValues.txt and assign each to the array.
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[7]
(d) The program needs to allow all players (maximum of 5) to select 4 cards from the 30 available.
A card can only be selected once, so the program needs to record which cards have already
been selected.
The function, ChooseCard():
•
takes as input an integer to represent an array index from 1 to 30
•
validates that the value is between 1 and 30 inclusive
•
checks if the card is available (it has not already been selected)
•
loops until an available card is selected
•
returns the index of the card if it is available.
Amend the program to store which cards have already been selected and write program
code for the function ChooseCard().
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[6]
(e) The main program needs to allow one player to select all their 4 cards.
(i)
Amend the main program to:
•
create an array, Player1, for player 1 of type Card
•
ask player 1 to input 4 integers using the function from part 3(d)
•
store the cards in Player1
•
output the number and colour of the 4 cards in Player1.
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[5]
(ii)
Test your program with the following test data:
Test 1: 1
5
9
10
Test 2: 2
2
3
4
4
5
Take a screenshot to show the output.
Copy and paste the screenshot into part 3(e)(ii) in the evidence document.
[1]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge

13
9618/41/M/J/21 · Question 3May/June 2021Paper 4131 marks
Loading question…
Question text

3
A computer game requires users to travel around a world to find and open treasure chests. Each
treasure chest has a mathematics question inside. The user enters the answer. The number of
points awarded depends on the number of attempts before the user gives the correct answer.
The program will be created using object-oriented programming (OOP).
The following class diagram describes the class TreasureChest.
TreasureChest
question : STRING
// stores the question
answer : INTEGER
// stores the answer
points : INTEGER
// stores the maximum possible number of
points available for this chest
constructor()
// takes question, answer and points as
parameters and creates an instance of an
object
getQuestion()
// returns the question
checkAnswer()
// takes the user’s answer as a parameter and
returns True if it is correct,
otherwise returns False
getPoints()
// takes the number of attempts as a
parameter and returns the number of
points awarded
(a) Create a new program.
Write program code to declare the class TreasureChest.
Do not write any other methods.
The attributes are private.
If you are using the Python programming language, include attribute declarations using
comments.
Save your program as question3.
Copy and paste the program code into part 3(a) in the evidence document.
[5]
(b) The text file TreasureChestData.txt stores data for five questions, in the order of
question, answer, points.
For example, the first three lines of the file are for the first question:
2*2 question
4 answer
10 points
Write program code for the procedure, readData() to:
•
read each question, answer and points from the text file
•
create an object of type TreasureChest for each question
•
declare an array named arrayTreasure of type TreasureChest
•
append each object to the array
•
use exception handling to output an appropriate message if the file is not found.
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[8]
(c) The main program repeats each question until the user inputs the correct answer. The
number of points awarded depends on the number of attempts before the user gives the
correct answer.
(i)
The class TreasureChest has a method getQuestion() that returns the question.
Write the method getQuestion().
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[1]
(ii)
The class TreasureChest has a method checkAnswer() that takes the user’s
answer as a parameter. It returns True if the answer is correct and False otherwise.
Write the method checkAnswer().
Save your program.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
[3]
(iii)
The class TreasureChest has a method getPoints() that takes the number of
attempts as a parameter.
•
If the number of attempts is 1, it returns the value of points.
•
If the number of attempts is 2, it returns the integer value of points divided by
2 (DIV 2).
•
If the number of attempts is 3 or 4, it returns the integer value of points divided by
4 (DIV 4).
•
If the number of attempts is not 1 or 2 or 3 or 4, it returns 0 (zero).
For example, a question is worth 100 points and the user took 2 attempts to give the
correct answer. The user is awarded 50 points (100 DIV 2).
Write the method getPoints().
Save your program.
Copy and paste the program code into part 3(c)(iii) in the evidence document.
[5]
(iv)
Write program code for the main program to:
•
call the procedure readData()
•
ask the user to enter a question number between 1 and 5
•
output the question that matches the question number entered by the user
•
check if the answer input by the user is correct using the method checkAnswer()
•
repeat the question until the user inputs the correct answer
•
count how many times the user attempted the question
•
use the method getPoints() to return the number of points awarded
•
output the number of points the user is awarded.
Save your program.
Copy and paste

Advertisement