Professional Documents
Culture Documents
[6]
2. A taxi firm is investigating replacing its drivers with self-driving cars.
The code for the self-driving system has been written using an object-oriented programming language.
i. Write a line of code to create an object called bollard of type Obstacle which is not moving and is 7.8 metres away in a
direction of 8 degrees.
[2]
[2]
[2]
3. A program is needed to plan the layout of a garden.
The program will allow the user to create an image of the garden, for example:
All items that can be added to the garden are declared as instances of the class GardenItem.
Attribute Description Example
itemName The name of the item Flowerbed
length The length of the item in metres 2
width The width of the item in metres 1
i. The constructor method sets the attributes to values that are passed as parameters.
Write pseudocode or program code to declare the class GardenItem and its constructor. All attributes should be private and
initialised through the constructor (e.g. daisies = new GardenItem("Flowerbed",2,1)).
ii. The trees in the garden layouts are defined by the class Tree. This class inherits from GardenItem.
The class Tree has the additional attributes: height, sun, shade.
If sun is true then the tree can grow in full sun, if it is false then it cannot.
If shade is true then the tree can grow in full shade, if it is false then it cannot.
The length and width of a tree are the same. Only one value for these measurements is passed to the constructor.
Write an algorithm, using pseudocode or program code, to declare the class Tree.
Declare all attributes as private.
[5]
iii. The Common Oak is a type of tree. It has a maximum height, length and width of 40 m. It can grow in sun and shade.
Write a statement, using pseudocode or program code, to declare an instance of tree for the Common Oak. Give the object the
identifier firstTree.
[4]
iv. The classes GardenItem and Tree use get and set methods to access and alter their private attributes.
Write the get method getItemName and set method setItemName for class GardenItem. The set method takes the new value
as a parameter.
[4]
v. The trees in the garden layouts are stored in a 1-dimensional array, treeArray. The array can store a maximum of 1000 items.
The array has global scope.
• The maximum height of a tree
• The maximum width of a tree
• Whether the tree can live in full sun
• Whether the tree can live in full shade.
It searches the array, treeArray, for all trees that do not exceed the maximum height and width, and that can grow in the conditions
available. If there are no suitable trees, a suitable message is output.
It outputs the name and details of the trees found in an appropriate message.
Call the get methods, getItemName, getHeight, getWidth, getSun, getShade, to access the attributes.
4(a). Livid Lizards is a computer game in which players get to fire lizards from a cannon to knock down walls. Players get to pick different
types of lizards, each with qualities and special powers.
The game is coded using an object-oriented language. Below is the code for the lizard class:
[3]
ii. Explain one way the game’s developers might use inheritance for Livid Lizards.
© OCR 2022. You may photocopy this Page 5 of 23 Created in ExamBuilder
page.
[3]
5. An object-oriented system is implemented to organize a company’s information about staff attendance. Classes, objects, methods and
attributes are used in this system.
Object
Method
Attribute
[3]
ii. Each worker has a name and an attendance figure which can be between 0 and 100.
Write a definition for a fully encapsulated customer class, providing both get and set methods for all attributes. You do not have to
write code for the constructor method.
[5]
6(a). Kim is writing an object-oriented program for a four player board game. The board has 26 squares that players move around, as
shown in Fig. 5.1.
Fig. 5.1
Each player takes it in turn to roll two dice. They then move that number of spaces on the board. If they roll a double (both dice have the
same value), they then take a card from the deck. The deck contains 40 cards that each include a sentence (such as “You have won the
lottery”). The sentence on the card determines if money is given or taken away from the player.
Each square (apart from Start and Miss a turn) has an animal associated with it that the player can purchase, if it has not been purchased
already, for example square 6 has a Squirrel. Fig. 5.2 shows an example of one of these animals. Once a player has purchased the
animal, any opposing player which subsequently lands on the square/animal has to pay a fine.
Each animal can be upgraded, with each upgrade the game charges more each time a player stops on them. For example, with no
upgrade the level 0 squirrel costs £10 when a player stops on it. If £1000 is paid to upgrade, the squirrel is then a level 1 animal and now
charges £50 for a stop.
When a player lands on, or passes the square ‘Start’ (position 0), they receive £500. If they land on ‘Miss a turn’ (position 13), they miss
their next turn.
i. A class, Player, stores the player's ID (P1, P2, P3, P4), their current board position and the amount of money they have.
Fig. 5.3 shows a class diagram for Player. A class diagram describes a class. It contains the class name, followed by the
attributes, then the methods.
Fig. 5.3
The constructor creates a new instance of Player, taking the player's ID as a parameter. The board position is set to 0, and money
to £2000.
Write, using pseudocode, the constructor method for the Player class.
[3]
Fig. 5.4
The constructor takes the required data as parameters and then sets currentLevel to 0, and assigns the parameters as the
remaining attributes for the new object.
Write, using pseudocode, the constructor method for the Animal class.
[4]
iii. Write, using pseudocode, the code to create an instance of Animal for the Squirrel shown in Fig. 5.2, positioned on square
number 6, for the constructor function you wrote in part (a)(ii).
[2]
(b). The board is stored as a 1D array, board, of data type Animal. The spaces at 0, and 13, are left as empty elements that are checked
using separate functions.
[6]
Explain the difference, benefits and drawbacks between passing by value and by reference. Recommend which should be used
for currentPlayer, justifying your decision.
[9]
Fig. 5.5
The array, deck, is treated as a queue, with a variable, headPointer, identifying the first card in the deck. When a card has been used, the
head pointer increases to move to the next position. If the end of the deck is reached, the head pointer returns to 0 and starts again.
7. * Procedural programming and object-oriented programming are two paradigms commonly used by programmers when developing
computer games.
Discuss the advantages of using object-oriented programming over procedural programming when developing computer games. You
should refer to inheritance, encapsulation and polymorphism in your answer.
[12]
8. Barney is writing a program to store data in a linked list. He is writing the initial program for a maximum of 10 data items.
Barney wants the nodes to be stored as objects using object-oriented programming. He designs the following class.
class: node
attributes:
private data : Real
private pointer : Integer
methods:
new (newData, newPointer)
getData()
getPointer()
setData(newData)
setPointer(newPointer)
i. Write an algorithm, using pseudocode or program code, to create the class node, its attributes and constructor.
[4]
ii. The class node, uses get methods and set methods.
[2]
9. A supermarket uses an object-oriented approach to organise items that it offers for sale. Part of the class definition for the
ItemForSale class is shown below.
class ItemForSale
public itemName
public price
public discount
…
endclass
The discount attribute represents a percentage discount on the price. The discount can be between 0 and 50 (inclusive). All new items
for sale initially have a discount value of 0.
[4]
ii. Write a line of code to create an object of type ItemForSale called mushypeas that has a name of “mushy peas” and a price of
£0.89
[3]
iii. Write the calculatePrice() method, which applies the percentage discount to the price and returns the new value.
[3]
10. A software development company is building an operating system for a mobile phone that is in the process of being designed.
* The code is written using an object-oriented programming (OOP) language. Discuss the advantages and disadvantages to the team of
developers of using OOP over procedural programming. You should refer to inheritance, encapsulation and polymorphism in your answer.
[9]
11. The layout for a 2-player board game is shown in Fig 2.1
Each time a player moves, a series of obstacles are to be added to the board.
On their turn, each player rolls two dice. The smaller number from the two dice is taken, and that many obstacles will appear on the board
in random locations.
[2]
[1]
iii. The recursive function could have been written using iteration.
Benefits
Drawbacks
[4]
[4]
v. If a position on the board is not occupied, its value is set to a blank string ("").
The current algorithm does not check if the random space generated is currently occupied.
Write a subroutine that takes the generated position of the board, checks if it is free and returns if free, or if occupied.
[3]
[1]
[1]
iii. Explain the meaning of the arrows in the diagram, using an example.
[2]
Jones.setSalary(12000)
[1]
v. You may assume that thisSeniorNurse has been correctly defined as an object of SeniorNurse, and that x is a number.
thisSeniorNurse.setSalary(x)
is valid even though setSalary( ) is not shown in the SeniorNurse part of the diagram.
[3]
13(a). A supermarket uses an object-oriented approach to organise items that it offers for sale. Part of the class definition for the
ItemForSale class is shown below.
class ItemForSale
public itemName
public price
public discount
…
endclass
The supermarket has previously had issues with discounts being set as values above 50.
Explain how encapsulation could be applied to the ItemForSale class to stop this problem from occurring.
You are not expected to write any code in your answer to this question.
[3]
(b). Some items in the supermarket are only available through home delivery. These items are the same as ItemsForSale with the
following exceptions:
• the supermarket also stores the location of the stock
• the percentage discount allowed is up to 75 rather than the standard 50.
The method new is used to denote the constructor for each class.
class building
private numberFloors
private width
private ………………………………………………………………………
public procedure new(pFloors, pWidth, pHeight)
numberFloors = ………………………………………………………………………
width = pWidth
height = pHeight
endprocedure
public function getNumberFloors()
return ………………………………………………………………………
(e). Christoff develops a new class to store the houses in one road. His class design is shown:
class : houseRoad
attributes:
private buildings(100) /array of class house
private numberBuildings /records the number
/of houses currently stored in the array
/buildings
methods:
new(building)
function getBuilding(buildingNum)
procedure newbuilding(pBuilding)
The method newbuilding() takes a new building as a parameter, and stores this in the next free space in the array buildings.
(f). Christoff wants to create a new house called houseOne. It has the properties: 2 floors, 8(m) width, 10(m) height, 3 bedrooms and 2
bathrooms.
The house is located on a road with the identifier limeAvenue of type houseRoad, houseOne is the first house in this road.
Write pseudocode or program code to declare the house houseOne, road limeAvenue and assign houseOne to the first array position in
the road.
[4]