User interface language: English | Español

Date November 2020 Marks available 3 Reference code 20N.2.SL.TZ0.10
Level SL Paper 2 Time zone no time zone
Command term Describe Question number 10 Adapted from N/A

Question

A company provides car parking services in large cities and needs a computer system to keep track of the number of vehicles using its parking areas.

When a vehicle arrives, its registration plate is recorded on the system and it is allocated a number that identifies where it should park. When the vehicle leaves, that space is made available for other vehicles to use.

Vehicles are identified by their unique registration plate, which is an alphanumeric code of eight characters (e.g. X1234567). This is clearly displayed on the vehicle.

A programmer created the classes ParkingArea and Vehicle to model the above situation.

public class ParkingArea {
private Vehicle vehicles[];
private String name;

ParkingArea(String name, int capacity) {
this.name = name;
if (capacity > 300) capacity = 300;
this.vehicles = new Vehicle[capacity];
}

String getName() {
return name;
}

public int getCapacity() {
return vehicles.length;
}

public int findVehicle(String reg) {
//find where the vehicle is located in the array and
//return the index not yet written
}
}
public class Vehicle {
private String registration;
private byte colour;
private boolean broken;

public final static byte BLACK=1;
public final static byte WHITE=2;
public final static byte BLUE=3;
public final static byte RED=4;
public final static byte GREEN=5;
private final static double ADMIN_FEE = 3;

public Vehicle() {}

public Vehicle(String registration) {
this.registration = registration;
}
public Vehicle(String registration, byte colour) {
this.registration = registration;
this.colour=colour;
}
public void setBroken(boolean broken) {
this.broken=broken;
}
public void setColour(byte colour) {
this.colour=colour;
}
public boolean getBroken() {
return broken;
}
public String getRegistration() {
return registration;
}
public double pay(int hours) {
// code to return admin fee - only if applicable
}
}

Outline one effect of using the modifier static when declaring a variable.

[2]
a.

Describe the relationship between the classes Vehicle and ParkingArea.

[3]
b.

Outline why it is necessary to use the keyword this in the setBroken method of the Vehicle class.

[2]
c.

Construct code to create an instance of the Vehicle class that has a registration of X1234567.

[2]
d.i.

Construct code that sets the colour of the object created in part (i) as black.

[2]
d.ii.

Markscheme

Award [2 max].
Static means it is the same for all instances of the class;
because it is contained in the class rather than in an instance of the class/object / it is defined at the class level;
Static means that less memory is taken up;
as only 1 memory allocation it created for all instances rather than 1 per instance;

a.

Award [3 max].
Award [1] for aggregation (accept Composition also);
Award [1] for An instance of ParkingArea can contain Vehicles;
Award [1] for A single ParkingArea can contain between 0 and 300 instances of Vehicle;

b.

Award [2 max].
To distinguish between 2 variables with the same name;
Allows the local/instance variable to be set to the parameter/class variable;

Note: Award [1] if the incorrect technical terms for variables are given but the idea of distinguishing between 2 types of variables with the same name is expressed.

c.

Award [2 max].

Award [1] for correctly declaring the variable as type Vehicle;
Award [1] for using the correct constructor;

e.g.

Vehicle v=new Vehicle("X1234567");

d.i.

Award [2 max].
Award [1] for calling the correct method on the correct instance variable name;
Award [1] for using the constant correctly, i.e. Vehicle.BLACK;

v.setColour(Vehicle.BLACK);

Note: accept the constant prefixed by the name of the instance instead of the class

d.ii.

Examiners report

Most students associated the modifier static with the idea of permanence in some form, but not all clearly understood the difference between class variables and instance variables.

a.

This was well-answered.

b.

The this modifier clearly causes confusion. Students realized that it in some way clarified the difference between the two occurrences of the variable broken.

c.

Many full marks for part (i), although some failed to put the String data in speech marks.

d.i.

Part (ii) showed a lack of experience with the byte type.

d.ii.

Syllabus sections

Option D: Object-oriented programming » D.1 Objects as a programming concept
Option D: Object-oriented programming

View options