Question 9
OCR Computer Science June 2024 Algorithms and programming (H446-02)A level40 marks
On the specification
- (a)
- 1.4 Data types, data structures and algorithms
- (b)(i)
- 2.2 Problem solving and programming
- (b)(ii)
- 2.2 Problem solving and programming1.4 Data types, data structures and algorithms
- (b)(iii)
- 2.1 Elements of computational thinking2.2 Problem solving and programming
- (c)(i)
- 2.2 Problem solving and programming
Question text
9 A game is being written that makes use of object-oriented programming. A prototype for one part of the game is being designed that includes a character, a road and a prize to collect. The road will have 50 spaces that a character can move along. Each space on the road will store a null value or a prize object for the user to collect. Each space is numbered sequentially from the first space (position 0) to the last space (position 49) and will not change during the game. As the player travels down the road, the position the player is on the road will be output. (a) The road is designed to be a 1-dimensional array with the identifier road. Explain why an array is a suitable data structure to represent the road. [3] (b) The characters and prizes are designed as separate classes. 10 of the spaces on the road will contain an instance of the class Prize. The other spaces will be empty. The class design for Prize is here. class: Prize attributes: private name : string private type : string private value : integer methods: new() getName() getType() getValue() new() is the constructor method. The name, type and value are passed to the constructor as parameters which then assigns these to the attributes. (i) The method getName() returns the data in the attribute name. Write the method getName() using pseudocode or program code. [2] (ii) A global 1-dimensional array, allPrizes, stores 10 objects of type Prize. The prize in index 3 has the name "Box", the type is "money" and the value is 25. Write pseudocode or program code to create a new object for this prize and store it in index 3 of allPrizes. [3] (iii) The game starts with 10 prizes. Each prize is allocated to one space on the road. An algorithm needs designing that will generate a random space on the road for each prize. Each road space can only store one prize. Describe the decisions that will need to be made in this algorithm and how these will affect the program flow. [3] (c) The class design for Character is here. class: Character attributes: private name : string private money : integer private experience : integer private roadPosition : integer methods: new() getName() getMoney() getExperience() getRoadPosition() changePosition() updateValues() The four get methods return the associated attribute. The number of moves is passed to changePosition() as a parameter. The method adds this value to the character’s position on the road. The type and value of an object are passed to updateValues() as parameters. If the object is money the value is added to the character’s money. If the type is experience the value is added to experience. If the type is neither money or experience no changes are made. (i) new() is the constructor method. The name of the character is passed into the constructor as a parameter. The constructor then initialises both the experience and road position of the character to 0 and initialises the amount of money to 5. Write the constructor method for Character using either pseudocode or program code. You do not need to declare the class, the attributes or any other methods. [5] (ii) The type and value of a prize are passed as parameters to the method updateValues. If the type is money the value is added to the character’s money. If the type is experience then the value is added to the experience. If the type is neither money or experience no changes are made. For example, for the Character player1: player1.updateValues("money",10) updates player1’s money by 10 player1.updateValues("experience",5) updates player1’s experience by 5 player1.updateValues("foo",9) has no effect on player1. Write pseudocode or program code for the method updateValues(). [5] (d) This incomplete pseudocode algorithm: • creates a new character with the name Jamal • loops until the character reaches the end of the road • generates a random number of spaces to move between 1 and 4 (including 1 and 4) • moves the character and checks if the new space has a prize • updates the character attributes if there is a prize • outputs the character’s new attribute values. Complete the pseudocode algorithm. character1 = new ("Jamal") newPosition = 0 while newPosition < move = random(1, 4) //this will generate a random number between 1 and 4 character1.changePosition(move) newPosition = character1.getRoadPosition() if newPosition < 50 and road[ ] != null then prizeType = road[newPosition].getType() valueAmount = road[newPosition].getValue() character1.updateValues( , valueAmount) print("Congratulations you are in position", newPosition, "and found", road[newPosition].getName()) print("Money =", character1.getMoney(), "and experience =", character1. ()) endif print("You reached the end of the road") [6] (e) The procedure displayRoad() outputs the contents of each space in the road. The number of each space is output with either: • the word "empty" if there is no prize • the name of the prize if there is a prize. 1 procedure displayRoad() 2 for x = 0 to 60 3 print("Space", y) 4 if road[x] == null then 5 print("empty") 6 elseif 7 print(road[x].getValue()) 8 endif 9 next x 10 endprocedure The algorithm contains errors. Give the line number of four different errors and write the corrected line for each error. Error 1 Error line 1 Correction Error 2 Error line 2 Correction Error 3 Error line 3 Correction Error 4 Error line 4 Correction [4] (f)* A programmer is going to create a prototype for one small part of the game. Both road and allPrizes will be needed throughout the whole prototype. The programmer is considering making these global arrays as she thinks it will reduce the development time. Another programmer has suggested that doing this may create some problems when the rest of the game is created at a later stage. Compare the use of global and local variables in this program. You should include the following in your answer: • the use of local and global variables • alternative methods to using global variables • the appropriateness of each to this program design. [9]