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

11 questions · Clear filters

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
1
9618/41/O/N/25 · Question 2Oct/Nov 2025Paper 4130 marks
Loading question…
Question text

2
A program stores data about trains and train stations using Object-Oriented Programming (OOP).
The class Train stores the data about the trains:
Train
TrainIDNumber : String
stores the train ID number
Route : Integer
stores the route number the train is travelling
initialises TrainIDNumber and Route to the parameter
Constructor()
values
GetTrainIDNumber()
returns the train ID number
GetRoute()
returns the route number the train is travelling
Write program code to declare the class Train and its constructor.
(a) (i)
Do not declare the other methods.
All attributes should be private.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question2_N25.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[4]
The methods GetTrainIDNumber() and GetRoute() return the appropriate attribute.
(ii)
Write program code for GetTrainIDNumber() and GetRoute()
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[3]
(b) The program is tested with four trains:
train ID number
route
12ADV
134
33ART
20
9FKF
3
21VBC
24
Write program code to declare an instance of Train for each of the four trains.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[2]
(c) The class Station stores the data about the stations:
Station
StationID : String
stores the station ID
NumberPlatforms : Integer
stores the number of platforms at the station
Trains[0:9] : Train
stores the trains currently at the station platforms
NumberTrains : Integer
stores the number of trains currently at the station
platforms
initialises StationID and NumberPlatforms
Constructor()
to the parameter values, initialises Trains to an
empty array and NumberTrains to 0
GetTrains()
returns a string containing data about the trains
currently at the station platforms
takes a Train parameter and stores it if there is a
AddTrain()
platform available; each platform can only have one
train
Write program code to declare the class Station and its constructor.
(i)
Do not declare the other methods.
All attributes should be private.
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 2(c)(i) in the evidence document.
[3]
The method AddTrain() takes a Train parameter. The method compares the
(ii)
attributes NumberTrains and NumberPlatforms to identify if there is a platform
available (a platform currently with no train).
The method returns FALSE if there are no platforms available.
If there is a platform available, the method:
•
stores the Train parameter in the array Trains
•
updates the appropriate attribute(s)
•
returns TRUE
Write program code for AddTrain()
Save your program.
Copy and paste the program code into part 2(c)(ii) in the evidence document.
[4]
The method GetTrains() returns the string "There are no trains" if there are no
(iii)
trains at the station platforms.
If there are trains at the station platforms, the method returns a string in the format:
The trains at station <StationID> are:
<TrainIDNumber> on route number <Route>
The line <TrainIDNumber> on route number <Route> is repeated for each train
at the station platforms.
For example: If the station with the station ID "NT1" has two trains with the train ID
numbers "48RTG", "6UFH", the method will produce this output:
The trains at station NT1 are:
48RTG on route number 43
6UFH on route number 12
Write program code for GetTrains()
Save your program.
Copy and paste the program code into part 2(c)(iii) in the evidence document.
[6]
(d) The program is tested with two stations:
number of
station ID
platforms
STH
2
NTH
1
Write program code to amend the main program to declare an instance of Station for
(i)
each of the two

2
9618/41/O/N/25 · Question 3Oct/Nov 2025Paper 4125 marks
Loading question…
Question text

A program stores records in the 2D array HashTable. Each record is stored at a specific index of
3
the array that is calculated using a hashing algorithm with the record’s key field.
The array has 100 × 10 elements. The hashing algorithm uses the key to generate an index
between 0 and 99 (inclusive). If two key fields generate the same index, there is a collision. Any
records that have a collision are stored in the next space in the same index.
For example: In this table two record keys generated the same hash value of 1. Four record keys
generated the same hash value of 3.
Index
0
1
2
3
4
5
…
9
0
record
1
record
record
2
3
record
record
record
record
4
…
99
The program uses Object-Oriented Programming (OOP).
(a) The class Record stores data about the records:
Record
Key : Integer
stores the integer key field for the data
Data : String
stores the string data
initialises Key and Data to its parameter values
Constructor()
The attributes Key and Data are public.
Write program code to declare the class Record and its constructor.
Use your programming language appropriate constructor.
Save your program as Question3_N25.
Copy and paste the program code into part 3(a) in the evidence document.
[2]
(b) The procedure InitialiseHashTable() initialises each element in the array to an empty
or null record.
Write program code to declare the global 2D array HashTable and the procedure
InitialiseHashTable()
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[2]
(c) The function Hash():
•
takes an integer key field as a parameter
•
calculates and returns the hash value of the key field.
The hash value is the result from the formula: key MOD 100
Write program code for Hash()
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[2]
(d) The procedure InsertData():
•
takes an object of type Record as a parameter
•
calculates the hash value for the parameter using the appropriate function
•
stores the parameter in the correct position in HashTable
You can assume there will be no more than 10 objects that generate the same hash value.
Write program code for InsertData()
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[4]
(e) The file "HashTableData.txt" stores 200 key values and string data items in the format:
key,string
For example, the first row in the text file is:
528,permission
The key is 528 and the string data is "permission"
The procedure ReadData():
•
opens the text file and reads each line
•
splits each line into the key and data
•
calls InsertData() with an object containing each key and matching data.
Write program code for ReadData()
Save your program.
Copy and paste the program code into part 3(e) in the evidence document.
[5]
The function GetRecord():
(f)
•
takes an integer key field as a parameter
•
calculates the hash value for the key field using the appropriate function
•
searches the hash table for the record with the matching key field
•
returns the data for the record if the record is found
•
returns "Not found" if the record is not found.
Write program code for GetRecord()
Save your program.
Copy and paste the program code into part 3(f) in the evidence document.
[5]
(g) The main program:
•
calls InitialiseHashTable() and ReadData()
•
takes five integer key fields as input from the user
•
calls GetRecord() with each input and outputs the return value.
(i)
Write program code for the main program.
Save your program.
Copy and paste the program code into part 3(g)(i) in the evidence document.
[3]
(ii)
Test your program with the following five inputs in the order given:
528
1128
1828
1062
39
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 3(g)(ii) in the evidence document.
[2]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and

3
9618/42/O/N/25 · Question 1Oct/Nov 2025Paper 4228 marks
Loading question…
Question text

1
A program stores data about birds using Object-Oriented Programming (OOP).
The class Bird stores the data about the birds:
Bird
DistancePerHour : Real
stores the number of kilometres per hour (km/h) the bird
can fly (between 0.0 and 100.0 inclusive)
Species : String
stores the species of the bird, for example Pigeon
XPosition : Real
stores the current horizontal position of the bird
YPosition : Real
stores the current vertical position of the bird
Constructor()
initialises Species and DistancePerHour to the
parameter values; initialises XPosition and YPosition
to 500.0
GetPosition()
returns a string message that contains the horizontal and
vertical position of the bird
GetSpecies()
returns the species of the bird
Move()
takes a direction and number of minutes flying (between
0 and 500 inclusive) as parameters and updates the
appropriate horizontal or vertical position
Write program code to declare the class Bird and its constructor.
(a) (i)
Do not declare the other methods.
All attributes should be private.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question1_N25.
Copy and paste the program code into part 1(a)(i) in the evidence document.
[4]
The method GetSpecies() returns the species of the bird.
(ii)
Write program code for GetSpecies()
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
[2]
The method GetPosition() returns a string of the bird’s data in the format:
(iii)
"X = " & <XPosition> & " Y = " & <YPosition>
An example string for a bird is:
X = 500.0 Y = 250.0
Write program code for GetPosition()
Save your program.
Copy and paste the program code into part 1(a)(iii) in the evidence document.
[3]
(iv)
If the bird is travelling north, the vertical position increases. If the bird is travelling south,
the vertical position decreases.
If the bird is travelling east, the horizontal position increases. If the bird is travelling west,
the horizontal position decreases.
The method Move():
•
takes the direction of travel as a parameter in the form ‘N’ for north, ‘S’ for south,
‘E’ for east or ‘W’ for west
•
takes the number of minutes that the bird has been flying (to the nearest minute) as
a parameter
•
calculates the distance travelled by the bird using the formula:
distance travelled = (distance per hour / 60) * minutes flying
•
updates the vertical or horizontal position using the distance calculated.
You do not need to validate the parameters.
Write program code for Move()
Save your program.
Copy and paste the program code into part 1(a)(iv) in the evidence document.
[5]
(b) The main program creates two instances of Bird
The first bird is the species ‘Cockatiel’ and flies 71.0 km/h.
The second bird is the species ‘Macaw’ and flies 56.0 km/h.
Write program code to declare and initialise the two birds.
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
[3]
(c) (i)
The main program needs to:
•
output a message that includes the species and current X position and Y position for
each bird
•
prompt the user to select one of the birds to move and take this as an input
•
prompt the user to enter the direction the bird has been travelling and take this as
an input
•
prompt the user to enter the time to the nearest minute that the bird has been
travelling and take this as an input
•
call the appropriate method to update the chosen bird’s position
•
output an update on the bird’s new position.
Each input needs to repeat until valid data is entered. All outputs must be meaningful.
Write program code to amend the main program.
Save your program.
Copy and paste the program code into part 1(c)(i) in the evidence document.
[8]
(ii)
Test your program four times to meet these criteria:
Test 1: The Cockatiel travels north for 60 minutes.
Test 2: The Macaw travels south for 30 minutes.
Test 3:

Advertisement
4
9618/43/O/N/25 · Question 1Oct/Nov 2025Paper 4331 marks
Loading question…
Question text

1
A program stores data for a board game using Object-Oriented Programming (OOP). The game
has objects that are placed on the board. Each object has a string code (for example "A") and an
integer value (for example, 2).
The class BoardObject stores the data about the objects that can be placed on the board:
BoardObject
Code : String
stores the board object’s code
Value : Integer
stores the integer value of the board object
Constructor()
initialises the attributes to the parameter values
GetCode()
returns Code
GetValue()
returns Value
Write program code to declare the class BoardObject 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 Question1_N25.
Copy and paste the program code into part 1(a)(i) in the evidence document.
[5]
The methods GetCode() and GetValue() return the appropriate attribute.
(ii)
Write program code for GetCode() and GetValue()
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
[3]
(iii)
The table shows the code and value of five board objects. The table has the variable
identifier where each of the objects are stored.
Variable
Code
Value
identifier
Object1
"A"
2
Object2
"B"
3
Object3
"C"
5
Object4
"D"
2
Object5
"E"
7
Write program code for the main program to instantiate each of the five board objects
and store them in the variables with the identifiers given.
Save your program.
Copy and paste the program code into part 1(a)(iii) in the evidence document.
[3]
(b) The board objects are placed on a board that is represented by a 0-indexed 2D array:
•
the board is a 10 x 10 grid
•
each position on the board is identified by a row and column number
•
rows are numbered 0 to 9
•
columns are numbered 0 to 9
•
board objects can be placed at a row and column position
•
each board position is initialised with an empty BoardObject, an empty BoardObject
has Code = "-" and Value = 0
For example, the element highlighted in the given board is in row 1 and column 3.
Row
Column
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
The class Board stores the data about the board and where the board objects are placed.
Board
TheBoard : ARRAY[0:9, 0:9] of BoardObject
stores the board contents as a 2D array of
10 x 10 elements of type BoardObject
Constructor()
initialises each of TheBoard elements to
an empty BoardObject with Code = "-"
and Value = 0
GetObject()
takes a row and column number as
parameters and returns the BoardObject
at the row, column position
SetObject()
takes a BoardObject, row number and
column number as parameters. Stores the
BoardObject in TheBoard at the given
row, column position
DisplayBoard()
outputs the code of each BoardObject
stored in TheBoard, one row at a time
Write program code to declare the class Board 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 1(b)(i) in the evidence document.
[4]
The method GetObject() takes a row number and column number as parameters.
(ii)
The method returns the BoardObject stored at the parameter position.
Write program code for GetObject()
Save your program.
Copy and paste the program code into part 1(b)(ii) in the evidence document.
[2]
The method SetObject() takes three parameters: a BoardObject, row number and
(iii)
column number.
The method stores the BoardObject parameter in the row, column position in
TheBoard
Write program code for SetObject()
Save your program.
Copy and paste the program code into part 1(b)(iii) in the evidence document.
[2]
The method DisplayBoard() outputs the Code of each BoardObject stored in
(iv)
TheBoard using GetCode()
Each row in TheBoard is output on one li

5
9618/41/O/N/24 · Question 2Oct/Nov 2024Paper 4134 marks
Loading question…
Question text

2
A computer program is designed to simulate horses doing show jumping. In show jumping, horses
jump over obstacles called fences. A horse successfully jumps a fence if it does not knock the
fence down.
The program is written using Object-Oriented Programming (OOP).
The class Horse stores data about the horses.
Horse
Name : STRING
stores the name given to the horse
MaxFenceHeight : INTEGER
stores the maximum height in cm that the horse can
jump, for example 132
PercentageSuccess : INTEGER
stores the percentage chance of a horse not knocking
down a fence, for example 70 represents a 70% chance
of jumping a fence successfully
initialises Name, MaxFenceHeight and
Constructor()
PercentageSuccess to its parameter values
GetName()
returns the name of the horse
GetMaxFenceHeight()
returns the maximum height the horse can jump
Success()
calculates and returns the percentage chance of a horse
successfully jumping a specific fence
Write program code to declare the class Horse and its constructor.
(a) (i)
Do not 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 as Question2_N24.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[4]
The get methods GetName() and GetMaxFenceHeight() each return the relevant
(ii)
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 array Horses stores objects of type Horse.
(i)
The program has two horses:
•
The horse named ‘Beauty’ can jump a maximum height of 150 cm and has a success
percentage rate of 72%.
•
The horse named ‘Jet’ can jump a maximum height of 160 cm and has a success
percentage rate of 65%.
Write program code to:
•
declare the array, Horses, local to the main program with space for two Horse
objects
•
store the two horses described in the array
•
output the name of both Horse objects from the array.
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[5]
(ii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 2(b)(ii) in the evidence document.
[1]
(c) The class Fence stores data about the fences. Each fence has a height in cm and a risk
number.
The risk is a whole number between 1 and 5 inclusive. A risk of 1 means the fence is the
easiest type to jump. A risk of 5 means the fence is the hardest type to jump.
Fence
Height : INTEGER
stores the height of the fence in cm
the height is between 70 and 180 inclusive
Risk : INTEGER
stores the risk as a whole number between 1 and 5
inclusive
initialises Height and Risk to its parameter values
Constructor()
GetHeight()
returns the height of the fence
GetRisk()
returns the risk of the fence
Write program code to declare the class Fence, its constructor and get methods.
(i)
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(c)(i) in the evidence document.
[4]
The array Course stores four Fence objects. The user inputs the height and risk for
(ii)
each fence, and these are validated before each fence is created.
Amend the main program to:
•
declare the local array Course
•
take as input the data for four fences from the user
•
loop the input until both the height and risk are valid for each fence
•
create an instance of Fence for each of the four valid fences and store each
instance in the array.
Save your program.
Copy and paste the program code into part 2(c)(ii) in the evidence document.
[5]
(d) The chance of a horse jumping a fence without knocking it down is calculated as follows.
If the height of the fence is more than the maxi

6
9618/42/O/N/24 · Question 1Oct/Nov 2024Paper 4229 marks
Loading question…
Question text

1
A computer game is designed for users to select characters. Each character can take part in a
group of events. Each group has five events. There are four types of event: jump, swim, run, drive.
The program is written using object-oriented programming.
(a) The class EventItem stores data about the events.
EventItem
EventName : STRING
stores the name of the event
Type : STRING
stores the type of event, either: jump, swim, run or drive
Difficulty : INTEGER
stores the difficulty of the event from 1 (easiest) to 5
(hardest)
Constructor()
initialises EventName, Type and Difficulty to its
parameter values
GetName()
returns the name of the event
GetDifficulty()
returns the difficulty of the event
GetEventType()
returns the type of event
Write program code to declare the class EventItem and its constructor.
(i)
Do not 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 as Question1_N24.
Copy and paste the program code into part 1(a)(i) in the evidence document.
[4]
The get methods GetName(), GetDifficulty() and GetEventType() each return
(ii)
the relevant attribute.
Write program code for the three get methods.
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
[3]
(b) The array Group stores five objects of type EventItem.
Write program code to declare Group local to the main program.
(i)
Save your program.
Copy and paste the program code into part 1(b)(i) in the evidence document.
[1]
(ii)
One group has the following events:
Event name
Event type
Event difficulty
Bridge
jump
3
Water wade
swim
4
100 mile run
run
5
Gridlock
drive
2
Wall on wall
jump
4
Write program code to create an instance of EventItem for each of the five events, and
store them in Group.
Save your program.
Copy and paste the program code into part 1(b)(ii) in the evidence document.
[3]
(c) The class Character stores data about the characters in the game.
Each character has a skill level for each type of event. The skill level is an integer between
1 and 5 inclusive. Skill level 1 is the lowest skill level, and skill level 5 is the highest skill level.
Character
CharacterName : STRING
stores the name of the character
Jump : INTEGER
stores the character’s skill level at events of type jump
Swim : INTEGER
stores the character’s skill level at events of type swim
Run : INTEGER
stores the character’s skill level at events of type run
Drive : INTEGER
stores the character’s skill level at events of type drive
Constructor()
initialises CharacterName, Jump, Swim, Run and
Drive to its parameter values
GetName()
returns the name of the character
CalculateScore()
takes the type of event and difficulty as parameters.
Calculates and returns the chance of the character
completing the event
Write program code to declare the class Character, its constructor and get method.
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 1(c) in the evidence document.
[4]
(d) The method CalculateScore() in the Character class calculates and returns the
percentage chance of a character completing an event.
When a character’s skill level is greater than or equal to the difficulty of that event, the
percentage chance of completing the event is 100%.
When a character’s skill level is less than the difficulty of that event, the character’s skill level
is subtracted from the difficulty of that event. This difference is used to identify the percentage
chance of success using this table:
Percentage chance
Difference
of success
1
80
2
60
3
40
4
20
For example:
•
A character has a skill level of 3 for events of type run.
•
An event of type run has a difficulty level of 5
•
The

7
9618/41/O/N/23 · Question 3Oct/Nov 2023Paper 4130 marks
Loading question…
Question text

3
A computer game is written using object-oriented programming.
The game has multiple characters that can move around the screen.
The class Character stores data about the characters. Each character has a name, a current X
(horizontal) position and a current Y (vertical) position.
Character
Name : STRING
stores the name of the character as a string
XPosition : INTEGER
stores the X position as an integer
YPosition : INTEGER
stores the Y position as an integer
Constructor()
initialises Name, XPosition and YPosition to its
parameter values
GetXPosition()
returns the X position
GetYPosition()
returns the Y position
SetXPosition()
adds the parameter to the X position
validates that the new X position is between 0 and 10 000
inclusive
SetYPosition()
adds the parameter to the Y position
validates that the new Y position is between 0 and 10 000
inclusive
Move()
takes a direction as a parameter and calls either
SetXPosition or SetYPosition with an integer value
(a) (i)
Write program code to declare the class Character and its constructor.
Do not declare the other methods.
Use your programming language’s appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question3_N23.
Copy and paste the program code into part 3(a)(i) in the evidence document.
[4]
(ii)
The get methods GetXPosition() and GetYPosition() each return the relevant
attribute.
Write program code for the get methods.
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
[3]
(iii)
The set methods SetXPosition() and SetYPosition() each take a value as a
parameter and add this to the current X or Y position.
If the new value exceeds 10 000, it is limited to 10 000.
If the new value is below 0, it is limited to 0.
Write program code for the set methods.
Save your program.
Copy and paste the program code into part 3(a)(iii) in the evidence document.
[4]
(iv)
The method Move() takes a string parameter: "up", "down", "left" or "right".
The table shows the change each direction will make to the X or Y position.
Use the appropriate method to change the position value.
Direction
Value change
up
Y position + 10
down
Y position − 10
left
X position − 10
right
X position + 10
Write program code for Move().
Save your program.
Copy and paste the program code into part 3(a)(iv) in the evidence document.
[4]
(b) Write program code to declare a new instance of Character with the identifier Jack.
The starting X position is 50 and the starting Y position is 50, the character’s name is Jack.
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[2]
(c) The class BikeCharacter inherits from the class Character.
BikeCharacter
Constructor()
takes Name, XPosition and YPosition as parameters
calls its parent class constructor with the appropriate values
Move()
overrides the method Move() from the parent class by
changing either the X position or the Y position by 20
instead of 10
(i)
Write program code to declare the class BikeCharacter and its constructor.
Do not declare the other method.
Use your programming language’s 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.
[3]
(ii)
The method Move() overrides the method from the parent class.
The table shows the change each direction will make to the X or Y position.
Direction
Value change
up
Y position + 20
down
Y position − 20
left
X position − 20
right
X position + 20
Write program code for Move().
Save your program.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
[2]
(d) Write program code to declare a new instance of BikeCharacter with the identifier Karla.
The starting X position is 100, the starting Y position is 50 and the character’s name is Karla.
Save your program.
Copy and paste the

8
9618/42/O/N/23 · Question 3Oct/Nov 2023Paper 4231 marks
Loading question…
Question text

3
A computer game is written using object-oriented programming.
The game has multiple characters.
The class Character stores data about the game characters. Each character has a name, date
of birth, intelligence value and speed value.
Character
CharacterName : STRING
stores the name of the character
DateOfBirth : DATE
stores the date of birth of the character
Intelligence : REAL
stores the intelligence value of the character
Speed : INTEGER
stores the speed value of the character
Constructor()
initialises CharacterName, DateOfBirth,
Intelligence and Speed to the parameter values
SetIntelligence()
assigns the value of the parameter to Intelligence
GetIntelligence()
returns the value of Intelligence
GetName()
returns the name of the character
ReturnAge()
calculates and returns the age of the character as an integer
Learn()
increases the value of Intelligence by 10%
(a) (i)
Write program code to declare the class Character and its constructor.
Do not declare the other methods.
Use your programming language’s appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question3_N23.
Copy and paste the program code into part 3(a)(i) in the evidence document.
[5]
(ii)
The get methods GetIntelligence() and GetName() return the attribute values.
Write program code for the methods GetIntelligence() and GetName().
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
[3]
(iii)
The method SetIntelligence() assigns the value of its parameter to the attribute.
Write program code for SetIntelligence().
Save your program.
Copy and paste the program code into part 3(a)(iii) in the evidence document.
[2]
(iv)
The method Learn() increases the current value of Intelligence by 10%.
Write program code for Learn().
Save your program.
Copy and paste the program code into part 3(a)(iv) in the evidence document.
[1]
(v)
The method ReturnAge() calculates and returns the age of the character in years as
an integer.
Assume that the current year is 2023 and only use the year from the date of birth for the
calculation. For example, the method returns 18 if the character was born on any date in
2005.
Write program code for the method ReturnAge().
Save your program.
Copy and paste the program code into part 3(a)(v) in the evidence document.
[2]
(b) (i)
Write program code to create a new instance of Character with the identifier
FirstCharacter.
The name of the character is Royal, date of birth is 1 January 2019, intelligence is 70
and speed is 30.
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
[2]
(ii)
Write program code to call the method Learn() for the character created in part 3(b)(i).
Output the name, age and intelligence of the character in an appropriate message.
Save your program.
Copy and paste the program code into part 3(b)(ii) in the evidence document.
[3]
(iii)
Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 3(b)(iii) in the evidence document.
[1]
(c) The class MagicCharacter inherits from the class Character. A magic character has an
element, for example, water. This element changes how they learn. The magic character’s
element is stored in the additional attribute Element.
MagicCharacter
Element : STRING
stores the element for the character
Constructor()
takes Element, CharacterName, DateOfBirth,
Intelligence and Speed as parameters
calls its parent class constructor with the appropriate values
initialises Element to its parameter value
Learn()
alters the intelligence of the character depending on the
character’s element
(i)
Write program code to declare the class MagicCharacter and its constructor.
Do not declare the other method.
Use your programming language’s appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and p

Advertisement
9
9618/41/O/N/22 · Question 2Oct/Nov 2022Paper 4131 marks
Loading question…
Question text

2
A computer program is being developed that uses a set of cards. The program is written using
object-oriented programming.
The program has two classes: Card and Hand.
The methods and attributes of these classes are shown:
Card
Number : INTEGER
stores the card number from 1 to 5 inclusive
Colour : STRING
stores the card colour: red, blue or yellow
Constructor()
takes a number and colour as parameters and sets
the private values to these parameters
GetNumber()
returns the card number
GetColour()
returns the card colour
Hand
Cards : ARRAY[0:9] OF Card
1D array of type Card
FirstCard : INTEGER
stores the position of the first card in the hand
NumberCards : INTEGER
stores the number of cards in the hand
Constructor()
takes five card objects as parameters, assigns each
card to the array Cards[], initialises FirstCard to 0
and NumberCards to 5
GetCard()
takes an index as a parameter and returns the card at
that index in the array
(a) (i)
Write program code to declare the class Card, its attributes and constructor.
Do not write program code for the get methods.
Use your programming language appropriate constructor.
All attributes must be private. If you are writing in Python, include attribute declarations
using comments.
Save your program as Question2_N22.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[5]
(ii)
Write program code for the class methods GetNumber() and GetColour().
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[3]
(iii)
The program is tested with the following cards:
Number
Colour
1
red
2
red
3
red
4
red
5
red
1
blue
2
blue
3
blue
4
blue
5
blue
1
yellow
2
yellow
3
yellow
4
yellow
5
yellow
Write program code to declare each of these cards as a variable of type Card in the
main program.
Save your program.
Copy and paste the program code into part 2(a)(iii) in the evidence document.
[2]
(b) (i)
Write program code to declare the class Hand, its attributes and constructor.
Do not write the get methods.
Use your programming language 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.
[6]
(ii)
The get method GetCard() takes an index as a parameter and returns the card stored
at that index in the array.
Write program code for the method GetCard().
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[2]
(iii)
Two players are declared with 5 cards each.
Player 1 has the cards: 1 red, 2 red, 3 red, 4 red, 1 yellow.
Player 2 has the cards: 2 yellow, 3 yellow, 4 yellow, 5 yellow, 1 blue.
Write program code to declare player 1 and player 2 as objects of type Hand, with the
cards indicated.
Save your program.
Copy and paste the program code into part 2(b)(iii) in the evidence document.
[2]
(c) The function CalculateValue() takes a player’s hand as a parameter and returns a score
calculated using the following rules:
•
If a card is red, 5 points are added to the player’s score.
•
If a card is blue, 10 points are added to the player’s score.
•
If a card is yellow, 15 points are added to the player’s score.
•
The number of each card in the hand is added to the player’s score.
(i)
Write program code for the function CalculateValue().
Assume that there are only 5 cards in the player’s hand in this function.
Save your program.
Copy and paste the program code into part 2(c)(i) in the evidence document.
[6]
(ii)
Amend the main program by writing program code to use the function
CalculateValue() for each of the two players. The player with the highest score wins.
Output an appropriate message to identify the winning player, or if the game was a draw
(both players have the same number of points).
Save your program.
Copy and paste the program code into part 2(c)(ii) in the evidence document.
[4

10
9618/42/O/N/22 · Question 2Oct/Nov 2022Paper 4231 marks
Loading question…
Question text

2
A computer game is being developed. The game has 10 different characters that are all active in
the game.
Part of the game is being written using object-oriented programming.
The class Character stores data about the characters. Each character has a name and the x
coordinate and y coordinate of their current position.
Character
Name : STRING
stores the name of the character
XCoordinate : INTEGER
stores the x coordinate
YCoordinate : INTEGER
stores the y coordinate
Constructor()
initialises Name, XCoordinate and YCoordinate from the
values passed as parameters
GetName()
returns the name of the character
GetX()
returns the x coordinate of the character
GetY()
returns the y coordinate of the character
ChangePosition()
takes XChange as an integer parameter and adds it to the
x coordinate
takes YChange as an integer parameter and adds it to the
y coordinate
(a) Write program code to declare the class Character and its constructor. Do not write
program code for the other methods.
Use your programming language appropriate constructor.
All attributes must be private. If you are writing in Python, include attribute declarations using
comments.
Save your program as Question2_N22.
Copy and paste the program code into part 2(a) in the evidence document.
[4]
(b) Write program code for the three get methods for the class Character.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[3]
(c) Write program code for the method ChangePosition().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) The main program has a 1D array of characters. Each character is stored as an object of type
Character.
The game has a maximum of 10 characters. The character names, x coordinates and y
coordinates are stored in the file Characters.txt in the order:
•
name
•
x coordinate
•
y coordinate.
For example, the first character in the file is named Amal, with the x coordinate 0 and the y
coordinate 2.
Amend the main program by writing program code to:
•
declare the array
•
read in all 10 characters from Characters.txt
•
store each character as an object in the array.
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[7]
(e) The main program needs to read in a character’s name from the user, search for the character
in the array and store the index of its position. It repeats until the user enters a name that
exists in the array.
Amend the main program by writing program code to perform this task.
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[5]
(f)
The user will enter a letter to identify the direction the chosen character from part 2(e) should
move.
•
If ‘A’ is input, the character moves left (x coordinate minus 1).
•
If ‘W’ is input, the character moves up (y coordinate plus 1).
•
If ‘S’ is input, the character moves down (y coordinate minus 1).
•
If ‘D’ is input, the character moves right (x coordinate plus 1).
Amend the main program by writing program code to:
•
take a letter as input until it is a valid move (A, W, S or D)
•
change the position of the character using the appropriate method.
Save your program.
Copy and paste the program code into part 2(f) in the evidence document.
[7]
(g) (i)
When a change to a character’s position has been made, the program needs to output
the character’s name and the new x and y coordinates of the character, in the format:
Qui has changed coordinates to X = 83 and Y = 0
Amend the main program by writing program code to perform these tasks.
Save your program.
Copy and paste the program code into part 2(g)(i) in the evidence document.
[2]
(ii)
Test your program by inputting the following four items of data in the order given:
THOMAS
qui
X
A
Take a screenshot of the output.
Copy and paste the screenshot into part 2(g)(ii) in the evidence document.
[1]

11
9618/41/O/N/21 · Question 2Oct/Nov 2021Paper 4130 marks
Loading question…
Question text

2
A program, written using object-oriented programming, stores pictures as objects.
The program stores the dimensions of the picture (width and height), the colour of the frame (e.g.
black), and a description of the picture (e.g. flowers).
The class has the following attributes and methods.
Picture
Description : STRING
//
stores
a description of the picture
Width : INTEGER
//
stores
the width e.g. 30
Height : INTEGER
//
stores
the height e.g. 40
FrameColour : STRING
//
stores
the colour e.g. black
Constructor()
// takes all four values as parameters and
sets them to the private attributes
GetDescription()
// returns the description of the picture
GetHeight()
// returns the height
GetWidth()
// returns the width
GetColour()
// returns the frame colour
SetDescription()
// takes the new description as a parameter
and writes the value to description
(a) The constructor takes the picture description, frame colour, height, and width as parameters
and sets these to the private attributes.
Write the program code to declare the class Picture 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 programming language, include attribute declarations using
comments.
Save your program as question 2.
Copy and paste the program code into part 2(a) in the evidence document.
[5]
(b) The four get methods return the associated attribute, for example, GetDescription()
returns the description of the picture.
Write the four get methods.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[3]
(c) The method SetDescription() takes a new description as a parameter, and writes this
value to the appropriate attribute.
Write the method SetDescription().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) Write program code to declare an array of type Picture with 100 elements.
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[1]
(e) The text file Pictures.txt stores the data for the pictures in the order: description, width,
height, colour.
For example, for the first picture in the text file:
Flowers is the description
45 is the width
50 is the height
black is the frame colour.
The data read into the program from the text file is stored in an array of type Picture.
The main program and the function will need to access the array data.
The function ReadData():
•
opens the file Pictures.txt
•
reads the data from the file
•
creates a new object of type Picture for each picture
•
writes each object to the array
•
raises an exception if the file cannot be found
•
counts and returns the number of pictures in the array.
Write program code for the function ReadData().
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[8]
(f)
The main program calls the function ReadData().
Write the main program.
Save your program.
Copy and paste the program code into part 2(f) in the evidence document.
[2]
(g) The main program needs to ask the user to input their requirements for a picture. The user
will enter the colour of the frame, the maximum width, and the maximum height of the picture.
The program will then search the array of pictures, and output the picture description, the
width, and the height of any picture that meets the user’s requirements.
The program should allow the user to input the colour in any case (e.g. Silver, silver, or
SILVER), and still output the correct results.
Edit the main program to perform the described actions.
Save your program.
Copy and paste the program code into part 2(g) in the evidence document.
[7]
(h) Test your program by inputting the following search criteria:
•
BLACK, 100, 100
•
silver, 25, 25
Take screenshots to show the output for both search criteria.
Copy and paste the screenshots into part 2(h) in the evidence document.

Advertisement