ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions

AH Computing Science 2023 Question 4

12 marks · Classes and objects

Making use of appropriate object‑oriented programming terminology, describe the effect of the following line of code. DECLARE newPlayer INITIALLY Player("French", "Player1626") A player’s power rating is calculated by working out the average of the speed, jump, shoot and size points. Speed Speed Jump Shoot Size Jump Shoot Size Using a programming language with which you are familiar, write code to implement the calcPower() method. Players can join a league with nine other players. The league table is sorted in descending order of power. Player1626 18.5 Player1762 17.25 Player2262 16.0 Player3562 14.75 Player1092 14.5 Player1827 12.75 Player1468 12.25 Player8372 11.5 Player6391 9.5 Player2864 9.0 Using appropriate object‑oriented terminology, explain the purpose of the code below. DECLARE league AS ARRAY OF Player INITIALLY [Null] * 10 SET league[0] TO newPlayer The incomplete code below is used to arrange the league table details in descending order of power by applying a bubble sort algorithm to the league variable defined in part (i) above. Line 2001 DECLARE numPlayers INITIALLY 9 Line 2002 DECLARE swapped INITIALLY TRUE Line 2003 WHILE ________________________ Line 2004 SET swapped TO FALSE Line 2005 FOR loop FROM 0 TO numPlayers-1 DO Line 2006 IF _________________________ Line 2007 SET temp TO league[loop] Line 2008 SET league[loop] TO league[loop +1] Line 2009 SET league[loop+1] TO temp Line 2010 _________________________ Line 2011 END IF Line 2012 END FOR Line 2013 SET numPlayers TO numPlayers − 1 Line 2014 END WHILE Using a programming language of your choice, write the code needed to complete Lines 2003, 2006 and 2010. During the game, players can collect power‑ups that allow them to change the type of ball being used. For example, the ball can become bigger or smaller, faster or slower, it can become an unstoppable fire ball or split into multiple balls. The UML class diagram below shows the classes used to represent each ball type that is possible. Fireball + fire() + rebound() Multiball ‑ numberBalls: Integer +Multiball() +cloneBall() Ball ‑ diameter: Integer ‑ speed: Integer ‑ appearance: String ‑ bounce: Boolean + Ball() + makeBig() + makeSmall() + makeFast() + makeSlow() + rebound() By referring to details in the UML class diagram above, explain what is meant by inheritance. The code used to define the rebound() method in the Ball and Fireball classes is shown below. Ball Method PROCEDURE rebound() SET THIS.bounce TO TRUE END PROCEDURE Fireball Method OVERRIDE PROCEDURE rebound() SET THIS.bounce TO FALSE END PROCEDURE Name the feature of object‑oriented programming illustrated by this example. A game is being developed where the player controls a footballer, playing against an online opponent. When the game is played for the first time, a player must provide a nationality and a username. A new player starts with 1 point for their speed, jump, shoot, size and power characteristics. By winning games, players can gain extra points in each of these characteristics. A simplified UML class diagram for a Player object is shown below. Player ‑ speed: Integer ‑ jump: Integer ‑ shoot: Integer ‑ size: Integer ‑ power: Real ‑ nationality: String ‑ username: String + updateSpeed() + updateJump() + updateShoot() + updateSize() + calcPower() + getUsername() + getPower() Some of the class declaration code is shown on the page opposite. CLASS Player IS { INTEGER speed, INTEGER jump, INTEGER shoot, INTEGER size, INTEGER real, STRING nationality , STRING username } METHODS CONSTRUCTOR(STRING nationality , STRING username) DECLARE THIS.speed INITIALLY 1 DECLARE THIS.jump INITIALLY 1 DECLARE THIS.shoot INITIALLY 1 DECLARE THIS.size INITIALLY 1 DECLARE THIS.power INITIALLY 1 DECLARE THIS.nationality INITIALLY nationality DECLARE THIS.username INITIALLY username END CONSTRUCTOR PROCEDURE updateSpeed() SET THIS.speed TO THIS.speed + 1 END PROCEDURE FUNCTION getUsername() RETURNS STRING RETURN THIS.username END FUNCTION FUNCTION getPower() RETURNS REAL RETURN THIS.power END FUNCTION ... END CLASS

Marking scheme

• This code is instantiating a new object of the Player class • It will assign the nationality and username using the parameters provided • The constructor code will assign the value 1 to all other properties Award 1 mark for creation of new object or instance (of the Player class) Award 1 mark for instantiating the object by assigning values Award 1 mark for assignment details Note: list of assigned values on its own – Award 1 mark only PROCEDURE calcPower() SET THIS.power TO (THIS.speed + THIS.jump + THIS.shoot + THIS.size) / 4 END PROCEDURE Award 1 mark for use of correct properties in calculation and assignment Award 1 mark for method coded as a procedure. Accept answers that don’t make use of THIS. This code creates an array of objects belonging to the Player class and sets the first element of the array to the newPlayer object. Award 1 mark for creation of array of Player objects Award 1 mark for assignment of first element in the array Line 2003 WHILE swapped (AND numPlayers >= Line 2006 IF league[loop].getPower() < league[loop+1].getPower() THEN Line 2010 SET swapped TO True Award 1 mark for Line 2003 and Line 2010 both correct Award 1 mark for correct comparison in Line 2006. Accept IF league[loop+1].getPower() ≥ league[loop].getPower() THEN as an alternative comparison. This mark can be awarded if even method has not been used. Award1 mark for correct use of getPower() method in Line 2006 Inheritance is when a hierarchy of classes that share core properties and methods can be defined. The parent or superclass becomes the blueprint for all related subclasses. In this situation, the Fireball and Multiball subclasses automatically inherit all properties and methods defined for the Ball superclass with the Multiball class having 2 additional properties. Award 1 mark for accurately referring the classes in the UML class diagram with superclass Ball with subclasses Fireball and Multiball both inheriting all the properties and methods of the Ball superclass. Polymorphism Award 1 mark for polymorphism Accept also overriding

Previous question: AH Computing Science 2023 Question 3

Next question: AH Computing Science 2023 Question 5