Past-paper question · 2017 October-November · Paper 41 · Q6(b)

Question 6(b) 2017 · October-November · Paper 41 · 5 marks

Original past-paper question for 6(B)
Original past-paper question for 6(B)
Original past-paper question for 6(B)
Original past-paper question for 6(B)
Reveal official mark-scheme answer Official mark-scheme answer for 6(B)Official mark-scheme answer for 6(B)

Official answer for Question 6

Part (b)

Question 1

mark per bullet to max 5

Question 5

  • constructor method heading and ending
  • Initialise all 40 by 40 elements of Grid as '' or equivalent
  • Loop 5 times«
  • «Creates a new instance of animal inside loop...
  • ...and adds it to array AnimalList
✓
•
✓
Call generate food and initialise StepCounter to 0
✓
Example Python
✓
def __init__ (self) :
✓
self.grid = [[' ' for i in range(40)] for j in range(40)]
✓
self.AnimalList = []
✓
self.StepCounter = 0
✓
for i in range(5) :
✓
newAnimal = Animal ()
✓
self.AnimalList.append(newAnimal)
✓
self.GenerateFood()
✓
Example VB
✓
Sub New()
✓
For x = 0 To 39
✓
For y = 0 To 39
✓
grid(x, y) = ""
✓
Next
✓
Next
✓
For z = 0 To 4
✓
AnimalList(z) = New Animal
✓
Next
✓
Call GenerateFood()
✓
End Sub
✓
9608/41
✓
Cambridge International AS/A Level – Mark Scheme
✓
October/November
✓
PUBLISHED
✓
2017
✓
Question
✓
Answer
✓
Marks

Mark scheme 9608_w17_ms_41.pdf →

Show text version of question

Question 6

Part (b)

Question 6

A programmer wants to create a computer simulation of animals searching for food in a desert.

The desert is represented by a 40 by 40 grid. Each position in the grid is represented by a pair of coordinates. 'A' represents an animal and 'F' represents food. At the start of the simulation, the grid contains 5 animals and 1 food source.

The following is an example of part of the grid.

0 1 2 3 4 ... 37 38 39 0 A .. 1 F .. 2 .. A 3 A .. ... .. .. .. .. .. .. .. .. .. 38 A .. A 39 ..

A
timer is used. In each time interval, each animal randomly moves 0 or 1 position in a random

direction. The program generates this movement by computing two random numbers, each of which can be –1, 0 or 1. The program adds the first random number to the across number and the second random number to the down number representing the animal’s position.

For example:

  • if 0 and 1 are generated, the across value does not change, the down value increases by 1
  • if –1 and 1 are generated, the across value decreases by 1, and the down value increases

by 1.

Each animal has an individual score. If the animal moves to a position in the grid with food ('F'):

  • the animal’s score increases by 1
  • the food disappears
  • one new animal ('A') is randomly generated and added to the grid (to a maximum of
  • 20 animals)

  • one new food ('F') is randomly generated and added to the grid.

The simulation is to be implemented using object-oriented programming.

The programmer has designed two classes, Desert and Animal.

The Desert class consists of:

  • attributes
  • Grid
  • StepCounter
  • AnimalList
  • NumberOfAnimals
  • methods
  • Constructor
  • IncrementStepCounter
  • GenerateFood
  • DisplayGrid

Each attribute consists of a value and a get and set method that allow access to the attributes.

Question 12

The following table describes the attributes and methods for the Animal class.

Identifier Data type Description Instantiate an object of the Animal class

  • Generate a pair of random numbers
  • Constructor() between 0 and 39.

  • Place animal at that random
  • position.

  • Initialise the animal’s score to 0.
  • Delete the food.
  • Increase the score of the animal that
  • called the method. EatFood() • Call the GenerateFood method of the Desert class.

  • Call the Constructor method of
  • the Animal class.

  • Call the
  • GenerateChangeInCoordinate method for each coordinate (across or down number) of the animal’s Move() position.

  • Moves the animal to the new space.
  • If there is food in the new position,

call the EatFood method. Score INTEGER Initialised to 0 Across INTEGER The across value, between 0 and 39 Down INTEGER The down value, between 0 and 39

Question 13

Part (b)

The Constructor method of the Desert class:

  • initialises an empty grid
  • creates 5 animal objects which are added to the AnimalList (an array of animal objects
  • currently on the grid)

  • generates one food
  • sets the StepCounter to 0.

Write program code for the Constructor method.

Programming language ............................................................................................................

Program code

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[5]

Question 15

Topic relevance

Why this question appears here

98% assigned-topic match · Further programming › Programming paradigms

Topic-match percentages estimate how closely each question fits the selected syllabus topic. They are a guide for choosing stricter or broader practice.

Permanent source page: Educate A Change · From 9608_w17_qp_41.pdf

Keep practising this topic