0% found this document useful (0 votes)
8 views10 pages

Cs Practical A Level

This document outlines the Cambridge International AS & A Level Computer Science Paper 4 Practical exam for May/June 2022, which consists of programming tasks involving object-oriented programming and data structures. It includes specific instructions for candidates on how to complete the tasks, save their work, and the programming languages allowed. The tasks involve creating classes and methods for a game element (Balloon) and implementing a stack data structure, among other programming challenges.

Uploaded by

Johnson Manguma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Cs Practical A Level

This document outlines the Cambridge International AS & A Level Computer Science Paper 4 Practical exam for May/June 2022, which consists of programming tasks involving object-oriented programming and data structures. It includes specific instructions for candidates on how to complete the tasks, save their work, and the programming languages allowed. The tasks involve creating classes and methods for a game element (Balloon) and implementing a stack data structure, among other programming challenges.

Uploaded by

Johnson Manguma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Cambridge International AS & A Level

COMPUTER SCIENCE 9618/41


Paper 4 Practical May/June 2022

2 hours 30 minutes

You will need: Candidate source files (listed on page 2)


* 9 3 7 2 6 1 2 3 7 6 *

evidence.doc

INSTRUCTIONS
● Carry out every instruction in each task.
● Save your work using the file names given in the task as and when instructed.
● You must not have access to either the internet or any email system during this examination.
● You must save your work in the evidence document as stated in the tasks. If work is not saved in the
evidence document, you will not receive marks for that task.
● You must use a high-level programming language from this list:
Java (console mode)
Python (console mode)
Visual Basic (console mode)
● A mark of zero will be awarded if a programming language other than those listed here is used.

INFORMATION
● The total mark for this paper is 75.
● The number of marks for each question or part question is shown in brackets [ ].

This document has 12 pages. Any blank pages are indicated.

DC (EF) 311848
© UCLES 2022 [Turn over
5

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]

© UCLES 2022 9618/41/M/J/22 [Turn over


6

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

© UCLES 2022 9618/41/M/J/22


7

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

© UCLES 2022 9618/41/M/J/22 [Turn over


2

Open the document evidence.doc

Make sure that your name, centre number and candidate number will appear on every page of this
document. This document must contain your answers to each question.

Save this evidence document in your work area as:

evidence_ followed by your centre number_candidate number, for example: evidence_zz999_9999

A class declaration can be used to declare a record.


If the programming language used does not support arrays, a list can be used instead.

A source file is used to answer Question 3. The file is called CardValues.txt

1 A program needs to use a stack data structure. The stack can store up to 10 integer elements.

A 1D array StackData is used to store the stack globally. The global variable StackPointer
points to the next available space in the stack and is initialised to 0.

(a) Write program code to declare the array and pointer as global data structures. Initialise the
pointer to 0.

Save your program as Question1_J22.

Copy and paste the program code into part 1(a) in the evidence document.
[3]

(b) Write a procedure to output all 10 elements in the stack and the value of StackPointer.

Save your program.

Copy and paste the program code into part 1(b) in the evidence document.
[3]

(c) The function Push() takes an integer parameter and returns FALSE if the stack is full. If the
stack is not full, it puts the parameter value on the stack, updates the relevant pointer and
returns TRUE.

Write program code for the function Push().

Save your program.

Copy and paste the program code into part 1(c) in the evidence document.
[6]

© UCLES 2022 9618/42/M/J/22


3

(d) (i) Edit the main program to test the Push() function. The main program needs to:

• allow the user to enter 11 numbers and attempt to add these to the stack
• output an appropriate message when a number is added to the stack
• output an appropriate message when a number is not added to the stack if it is full
• output the contents of the stack after attempting to add all 11 numbers.

Save your program.

Copy and paste the program code into part 1(d)(i) in the evidence document.
[5]

(ii) Test your program from part 1(d)(i) with the following 11 inputs:

11 12 13 14 15 16 17 18 19 20 21

Take a screenshot to show the output.

Copy and paste the screenshot into part 1(d)(ii) in the evidence document.
[1]

(e) The function Pop() returns −1 if the stack is empty. If the stack is not empty, it returns the
element at the top of the stack and updates the relevant pointer.

(i) Write program code for the function Pop().

Save your program.

Copy and paste the program code into part 1(e)(i) in the evidence document.
[5]

(ii) After the code you wrote in the main program for part 1(d)(i), add program code to:

• remove two elements from the stack using Pop()


• output the updated contents of the stack.

Test your program and take a screenshot to show the output.

Copy and paste the screenshot into part 1(e)(ii) in the evidence document.
[2]

© UCLES 2022 9618/42/M/J/22 [Turn over


6

(c) The following pseudocode function uses recursion to perform a binary search in the first row
of the array, for the value SearchValue in the array SearchArray.

The function returns −1 if the item was not found, or it returns the index where it is found.

There are six incomplete statements.

FUNCTION BinarySearch(SearchArray, Lower, Upper, SearchValue)RETURNS


INTEGER
IF Upper >= Lower THEN
Mid (Lower + (Upper – 1)) DIV …………………………………
IF SearchArray[0, Mid] = ………………………………… THEN
RETURN …………………………………
ELSE
IF SearchArray[0, Mid] > SearchValue THEN
RETURN BinarySearch(SearchArray, …………………………………, Mid – 1,
SearchValue)
ELSE
RETURN BinarySearch(SearchArray, Mid + 1, …………………………………,
SearchValue)
ENDIF
ENDIF
ENDIF
RETURN …………………………………
ENDFUNCTION

Note: the arithmetic operator DIV performs integer division, e.g. the result of 10 DIV 3 will be 3.

(i) Write program code for the recursive function BinarySearch().

Save your program.

Copy and paste the program code into part 2(c)(i) in the evidence document.
[8]

(ii) In the main program, test the function BinarySearch() twice, outputting the returned
value each time.

One test should be for a number that is in the first line of the array.
One test should be for a number that is not in the first line of the array.

Take a screenshot to show the output.

Copy and paste the screenshot into part 2(c)(ii) in the evidence document.
[2]

© UCLES 2022 9618/42/M/J/22


4

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]

© UCLES 2023 9618/41/M/J/23


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 SetCurrentSpeed() and
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]

© UCLES 2023 9618/41/M/J/23 [Turn over


6

(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 program code for the method IncreaseSpeed()

Save your program.

Copy and paste the program code into part 2(b)(ii) in the evidence document.
[4]

© UCLES 2023 9618/41/M/J/23

You might also like