Date | November 2019 | Marks available | 2 | Reference code | 19N.2.SL.TZ0.10 |
Level | SL | Paper | 2 | Time zone | no time zone |
Command term | State | Question number | 10 | Adapted from | N/A |
Question
To improve customer satisfaction, a supermarket chain wants to create an object-oriented program (OOP) to simulate the lines of customers at the check-outs in their point of sale (POS) system.
This point of sale (POS) system consists of several check-out counters. After filling their shopping carts with items, customers line up at one of the check-out counters. In most cases, they wait in line until it is their turn to pay.
Three real-world objects are implemented using the following classes:
The UML diagram for the class POSline
is provided below.
State the relationship between the POSsystem
and POSline
objects.
Draw a diagram to show the relationship between the objects POSsystem
, POSline
and Cart
. You are not required to draw a complete UML diagram.
Define the term identifier.
Distinguish between a class and an instantiation. You must make reference to the UML provided.
State the code fragment that instantiates an array line
of 20 Cart
objects.
Construct the method public void joinLine(Cart newCart)
that adds a newCart
to line
in the next empty array
location. You may assume that line
is not full.
Construct the method public Cart leaveLine(int n)
that removes the cart at position n
from line
by shifting down all the array entries bigger than n
, before returning the indicated Cart
object. You may assume that a cart exists at position n
.
Markscheme
Award [1 max].
Aggregation;
POSsystem has a (many) POSline(s);
Allow 1 to many;
Award [2 max].
Award [1] for one POSsystem connecting to multiple POSlines;
Award [1] for one POSline connecting to multiple Carts;
Example answer:
Allow diagram showing aggregation for both (diamond at POSsystem end or 1 to many)
Award [1 max].
The (unique) name of a program element (variable / method / class);
Must have “name” or equivalent.
Award [3 max]. [1 mark each] for definitions of a class and an object and [1 mark] for an example relating to the UML diagram that involves both class and object for either the POSline or the Cart classes.
The class is the blueprint / template of an object / defines the actions and properties of an object / an abstract representation of an object;
An instantiation is the (creation of an) actual object / example of an object / object filled with data / assigned space in memory;
The class POSline
stores instantiated objects of the Cart
class;
The class POSline
uses the constructor to set the (object’s) variables;
An instance of POSline
has values for id, active and line;
Allow similar example for the Cart object;
Award [2 max].
Award [1] for using new
;
Award [1] for Cart[20]
;
Allow with or without private
.
private Cart[] line = new Cart[20];
Award [3 max].
Award [1] for correct loop construction (for a while loop this includes initialization and incrementation);
Award [1] for correct condition (can be within the loop construct);
Award [1] for correct assignment;
public void joinLine(Cart newCart){
int i=0;
while (this.line[i] != null) {i++;}
this.line[i] = newCart;
}
Notes: Do not penalize the absence of the keyword “this” in any question.
With for loops, allow variations of line.length() – mark the logic
Further examples:
Note: if the answer takes size of array into account then the given condition must be correct.
public void joinLine(Cart newCart)
{
int i=0;
boolean placed = false;
while (i<20 && placed = false)
{
if this.line[i]= null;
{
this.line[i] = newCart;
placed = true;
}
i = i+1;
{
}
public void joinLine(Cart newCart)
{
for (int i; i<20; i++)
{
if (this.line[i] == null)
{
this.line[i] = newCart;
break;
}
{
}
Award [4 max].
Award [1] for declaring, initialising and returning a Cart
variable;
Award [1] for correct loop (including initialisation and incrementation);
Award [1] for assigning / shifting;
Award [1] for assigning line[19]
to null;
public Cart leaveLine(int n){ // given in the stem
Cart result = this.line[n];
for (int i=n; i<19; i++){
this.line[i] = this.line[i+1];
}
this.line[19] = null;
return result;
}
Examiners report
This was well-answered.
Those who answered part (a) correctly tended to answer this well. Most used uml diagrams to show the relationships either with words or by using the correct arrows.
Generally well-answered.
This was one of several questions in which reference to the scenario was required. This is to reward the candidates' understanding as opposed to simple memory recall. Not all candidates managed to make this connection.
These 3 question parts tended to divide the candidates into those who were comfortable with OOP construction as those who weren't. To a certain extent this seemed to be school dependent.
Straight-forward for those competent with Java.
These 3 question parts tended to divide the candidates into those who were comfortable with OOP construction as those who weren't. To a certain extent this seemed to be school dependent.
Generally well-answered, helped partly by the assumption that the array was not full. It was particularly pleasing to see the answers that included a while loop as candidates seem to be reluctant to use this type of loop construction even when logically it makes sense to do so.
These 3 question parts tended to divide the candidates into those who were comfortable with OOP construction as those who weren't. To a certain extent this seemed to be school dependent.
Few managed to achieve full marks as four separate operations were required. The assignment statement was linked to the loop conditions there were interesting variations - but is was essential not to try to access an array index that was out of bounds and therefore did not exist. Many omitted setting the last value to null.