Question 9

OCR Computer Science June 2023 Algorithms and programming (H446-02)A level40 marks

On the specification

(a)(i)
2.1 Elements of computational thinking
(a)(ii)
2.1 Elements of computational thinking
(b)(i)
2.2 Problem solving and programming
(b)(ii)
2.2 Problem solving and programming
(b)(iii)
2.2 Problem solving and programming
(c)
2.2 Problem solving and programming1.4 Data types, data structures and algorithms
(d)
2.2 Problem solving and programming
(e)
1.2 Software and software development
(f)
2.2 Problem solving and programming
Question text

9 A text-based computer game allows a user to dig for treasure on an island. The island is designed as a grid with 10 rows and 20 columns to store the treasure. Each square is given an x and y coordinate. Some of the squares in the grid store the name of a treasure object. Each treasure object has a value, e.g. 100 and a level, e.g. "Bronze." (a) The computer game makes use of abstraction. (i) Describe what is meant by the term abstraction and give an example of how abstraction can be used in the treasure game. Description: Example: [3] (ii) Give three benefits of using abstraction when writing a program. 1 2 3 [3] (b) The treasure game is being programmed using an object-oriented paradigm. A class, Treasure, is used to store the treasure objects. The design for the Treasure class, its attributes and methods is shown here. class: Treasure attributes: private value : integer private level : string methods: new() function getValue() function getLevel() (i) The constructor method takes a value as an integer, e.g. 100, and a level, e.g. "bronze", as parameters and assigns these to the attributes. Write pseudocode or program code to declare the class Treasure. You should define the attributes and constructor method in your answer. You do not need to write the get methods. [5] (ii) The get method getLevel() will return the appropriate attribute. Write the method getLevel() using either pseudocode or program code. [2] (iii) Describe the object-oriented programming technique being used in part 9(b)(ii). [2] (c) A class, Board, is used to store the 10 row (x coordinate) by 20 column (y coordinate) grid. The design for the Board class, its attributes and methods is shown here. class: Board attributes: private grid : Array of Treasure methods: new() function getGridItem(x, y) function setGridItem(x, y, treasureToInsert) The constructor initialises each space in the grid to a treasure object with value as -1 and level as an empty string. Complete the following pseudocode for the constructor method. public procedure new() for row = to 9 for column = 0 to [row, column] = new Treasure( ,"") next next row endprocedure [5] (d) A procedure, guessGrid(): • takes a Board object as a parameter • accepts the row (x) and column (y) coordinates from the user • outputs "No treasure" if there is no treasure found at the coordinate (level is an empty string) • if there is treasure at that coordinate, it outputs the level and the value of the treasure in an appropriate message. Write the procedure guessGrid() using either pseudocode or program code. [7] (e) Describe two benefits of using an object-oriented paradigm rather than a procedural paradigm. 1 2 [4] (f)* The main program initialises a new instance of Board. The programmer is considering declaring this as a global variable or as a local variable and then passing this into the subroutines that control the game. Compare the use of variables and parameters in this game. You should include the following in your answer: • what is meant by a local variable and global variable • how local and global variables can be used in this program • the use of passing parameters by value and by reference. [9]