User interface language: English | Español

Date May 2018 Marks available 5 Reference code 18M.2.SL.TZ0.11
Level SL Paper 2 Time zone no time zone
Command term Construct Question number 11 Adapted from N/A

Question

The details of hotel stays during the current year are stored in the variable allVisits which is an array of the Visits class. allVisits is used in determining the total points awarded in the current year.

The Visits class is outlined below:

public class Visits
{
private String hotelCode; // id of the hotel
private int days; // number of days of the visit

public
Visits(String h, int d)
{
hotelCode = h;
days = d;
}

public int
getDays()
{
return days;
}
}

The main (driver) class manages the Points and Visits classes. It contains the following code:

Points[] allPoints = new Points[10000]; // declared globally

allPoints[0] = new Points("m100");
allPoints[1] = new Points("m101",5000);
allPoints[2] = new Points("m102",2000);

Visits v1 = new Visits("h003", 3);
Visits v2 = new Visits("h013", 1);
Visits v3 = new Visits("h013", 2);
Visits v4 = new Visits("h005", 6);

allPoints[0].addVisit(v1);
allPoints[0].addVisit(v2);
allPoints[0].addVisit(v3);
allPoints[0].addVisit(v4);
allPoints[1].addVisit(v1);
allPoints[1].addVisit(new Visits("h004",6));

Construct a UML diagram for the Visits class.

[3]
a.

State the output given by the following statement:

System.out.println(allPoints[2].getMemberId());

[1]
b.i.

State the output given by the following statement:

System.out.println(allPoints[0].getBonusPoints());

[1]
b.ii.

State the output given by the following statement:

System.out.println(allPoints[1].getAllVisits(1).getDays());

[1]
b.iii.

Construct the method calculateTotalPoints(), in the Points class, which will calculate and return the total number of points awarded so far in the current year.

[5]
c.

Markscheme

Award [1] for three compartments, [1] for correct + and −, and [1] for correct contents.

Note:

a.

m102;

b.i.

0;

b.ii.

6;

b.iii.

Example 1:

public int calculateTotalPoints()
{
int totalPoints = 0;
for (int x = 0; x < y; x++)
{
totalPoints = totalPoints + allVisits[x].getDays();
}
totalPoints = totalPoints * 1000 + bonusPoints;
return totalPoints;
}

Award marks for correctly including the following:
Signature + matching return;
Loop through the number of visits (y); // do not allow length statements;
Any use of allVisits array;
Correct update of totalPoints (with or without bonusPoints);
Inclusion of bonus points outside of the loop (or if the loop is absent);

Example 2:

public int calculateTotalPoints()
{
int totalDays = 0;
for (int x = 0; x < y; x++)
{
totalDays = totalDays + allVisits[x].getDays();
}
totalPoints = totalDays * 1000 + bonusPoints;
return totalPoints;
}
c.

Examiners report

[N/A]
a.
[N/A]
b.i.
[N/A]
b.ii.
[N/A]
b.iii.
[N/A]
c.

Syllabus sections

Option D: Object-oriented programming » D.3 Program development
Show 45 related questions
Option D: Object-oriented programming

View options