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

24 questions · page 2 of 2 · Clear filters

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
16
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

17
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

18
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

Advertisement
19
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

20
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]

21
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]

22
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

23
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
24
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