Date | May 2018 | Marks available | 3 | 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.
State the output given by the following statement:
System.out.println(allPoints[2].getMemberId())
;
State the output given by the following statement:
System.out.println(allPoints[0].getBonusPoints())
;
State the output given by the following statement:
System.out.println(allPoints[1].getAllVisits(1).getDays())
;
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.
Markscheme
Award [1] for three compartments, [1] for correct + and −, and [1] for correct contents.
Note:
- Allow variations in the format, but must use + / −.
- Accept additional getters/setters, but the given content must be present.
m102;
0;
6;
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;
}