Date | May 2018 | Marks available | 6 | Reference code | 18M.2.HL.TZ0.18 |
Level | HL | Paper | 2 | Time zone | no time zone |
Command term | Construct | Question number | 18 | Adapted from | N/A |
Question
Full details of the customers are stored as objects of the Customer class. This class is partially shown below:
public class Customer
{
private String memberId;
private String email; //email address (assume only 1 per customer)
public Customer(String a, String b)
{
memberId = a;
email = b;
}
public String getMemberId()
{
return memberId;
}
public String getEmail()
{
return email;
}
}
The objects can be accessed through the linked list allCustomers
which is declared in the main (driver) class as follows:
LinkedList<Customer> allCustomers = new LinkedList<Customer>()
Outline why a linked list structure has been chosen for allCustomers
.
Construct the method goldMails()
that will return an ArrayList
containing the email addresses of all current “Gold” members. You should make use of any previously defined methods.
Markscheme
Example 1:
The link list will enable space to be allocated dynamically/will only take up the space required;
Because the number of customers is unknown / customers may join or leave the loyalty scheme during the year;
Example 2:
Customers / objects / nodes can be easily added or deleted;
So customers may join or leave the loyalty scheme during the year;
public ArrayList goldMails()
{
ArrayList al = new ArrayList(); // Allow use of <String> identifier
int s = allCustomers.size();
int x = 0;
while(!(allPoints[x] == null)) // Allow(allPoints[x]!=null)
{
if (allPoints[x].isGold())
{
String a = allPoints[x].getMemberId();
for (int y = 0; y < s; y++)
{
String b = allCustomers.get(y).getMemberId();
if (a.equals(b)) // allow =
al.add(allCustomers.get(y).getEmail());
}
}
x = x + 1
}
return al;
}
Award marks as follows:
Declaring a new ArrayList
(with or without the String identifier);
Looping through the allPoints
array;
Testing for “Gold”;
Loping through the allCustomers
linked list (using the size or an iterator);
Correct check for identical ids;
Adding the email to the ArrayList
;
Returning the arraylist;
Note:
- Do not allow any methods that are not part of the LinkedList or ArrayList library classes – e.g. the length function for the linked list is invalid.
- Do not penalize more than once for omitting the “gets”.
- Allow the for loop:
for
(Customer currCust : allCustomers
). - Allow for loop instead of a while loop.