User interface language: English | Español

Date November 2018 Marks available 1 Reference code 18N.2.HL.TZ0.16
Level HL Paper 2 Time zone no time zone
Command term Define Question number 16 Adapted from N/A

Question

A different airport has two runways for arriving flights. Two objects of the LinkedList class, named runway1 and runway2, are associated with these two runways.

Arriving flights are added to a list depending on the runway they are assigned to.

Both linked lists contain Arrival objects, corresponding to arriving flights, and are sorted by ETA.

The following diagram represents runway1.

Consider the following method.

void process(LinkedList<Arrival> myList, int i)
{ if (i < myList.size())
{ process(myList, i + 1);
System.out.println(myList.get(i).getETA());
}
}

Outline an advantage of using a library.

[2]
a.

Evaluate the use of dynamic linked lists to static arrays when managing arriving airplanes.

[4]
b.

Sketch the resulting linked list when an airplane with ID RO225, STA 12:05 and delay 0 is added to this list.

[2]
c.

Define the term recursion.

[1]
d.i.

Trace the call process(runway1,0) given the diagram of runway1 as drawn above. Copy and complete the following table.

[4]
d.ii.

In case of bad weather it is possible that one of the runways has to be closed and the two linked lists (runway1 and runway2) need to be merged using a method mergeLists() into one new linked list runway.

Construct the method public LinkedList<Arrival> mergeLists() that merges the two sorted linked lists in to one sorted linked list result which needs to be returned. You may assume that runway1 and runway2 are accessible to the method and do not need to be passed. The original two lists are allowed to become null as a result of the merging. 

In answering this question, you may use the following methods from the JETS LinkedList class, in addition to any method provided or developed in this exam paper.

addFirst(<object>)   // adds the object at the head of the list
addLast(<object>) // adds the object at the tail of the list
getFirst() // returns the head object in the list
getLast() // returns the tail object in the list
removeFirst() // removes and returns the head object
removeLast() // removes and returns the tail object
size() // returns the number of objects in the list
isEmpty() // returns true if the list is empty
[7]
e.

Markscheme

Award up to [2 max].
Award [1] for identifying an advantage.
Award [1] for an elaboration of the advantage.

Example answers:
Convenience;
Because implementations of common tasks are available;
Reliability;
because these implementations are fully developed, functional and robust.

a.

Award up to [4 max].
Award [1] for identifying an issue and [1] for a valid comparison related to the issue.
Mark as [2] and [2]

b.

Award up to [2 max].
Award [1] for a good attempt (for example the new node being added as the 3rdnode in the list).
Award [2] for a fully correct diagram.

c.

When a method calls on itself.

d.i.

Award up to [4 max].
Award [1] for correct recursion levels i including i = 3.
Award [1] for the correct flight id's (with or without the line for i = 3.
Award [1] for 12:55 and 11:05 in the correct order.
Award [1] for 12:30 (ETA) in the correct order.
Note: that there are many ways to trace a recursive algorithm.

d.ii.

For answers without iterators
Award up to [7 max].
Award [1] for correctly instantiating the LinkedList variable result.
Award [1] for declaring (and instantiating) all other variables.
Award [1] for correct main loop.
Award [1] for correctly comparing the first Arrivals in the two lists.
Award [1] for correctly removing the earlier Arrival from its original list.
Award [1] for correctly adding this Arrival to the end of the resulting list.
Award [1] for attempting to add the remainder of a non-empty linked list.
Award [1] for correct loop for copying the remainder of runway1.
Award [1] for correctly removing/adding the remainder of runway1.
Award [1] for also copying a possible remainder of runway2.

Example without iterator:

public LinkedList<Arrival> mergeLists()
{
LinkedList<Arrival> result = new LinkedList<Arrival>();
Arrival temp = null;
while (!runway1.isEmpty() && !runway2.isEmpty())
{
if (runway1.getFirst().compareWith(runway2.getFirst())<0)
{ temp = runway1.removeFirst(); }
else
{ temp = runway2.removeFirst(); }
result.addLast(temp);
}

while
(!runway1.isEmpty())
{ temp = runway1.removeFirst();
result.addLast(temp);
}
while (!runway2.isEmpty())
{ temp = runway2.removeFirst();
result.addLast(temp);
}

return
result;
}

Alternative example without iterator:

public LinkedList<Arrival> mergeLists()
{
LinkedList<Arrival> result = new LinkedList<Arrival>();
Arrival head1 = runway1.peekFirst();
Arrival head2 = runway2.peekFirst();

while
((head1 != null) && (head2 != null))
{ if (head1.compareWith(head2) <= 0) // or <
{ result.addLast(head1);
runway1.removeFirst();
head1 = runway1.peekFirst();
}
else
{ result.addLast(head2);
runway2.removeFirst();
head2 = runway2.peekFirst();
}
}
while (!runway1.isEmpty())
{
result.addLast(runway1.removeFirst());
}
while (!runway2.isEmpty())
{
result.addLast(runway2.removeFirst());
}
return result;
}

For answers that use iterators
Award up to [7 max].
Award [1] for correctly instantiating the LinkedList variable result.
Award [1] for declaring and instantiating iterators and all other variables.
Award [1] for correct main loop.
Award [1] for correctly comparing the first Arrivals in the two lists.
Award [1] for correctly adding the earlier Arrival to the end of the resulting list.
Award [1] for testing iter.hasNext() and loop termination.
Award [1] for attempting to add the remainder of a non-empty linked list.
Award [1] for correct loop used for copying the remainder of runway1.
Award [1] for correctly copying the remainder of runway1.
Award [1] for also copying a possible remainder of runway2.

Example with iterator:

public LinkedList<Arrival> mergeLists()
{
LinkedList<Arrival> result = new LinkedList<Arrival>();
ListIterator<Arrival> iter1 = runway1.listIterator();
ListIterator<Arrival> iter2 = runway2.listIterator();
Arrival curr1 = null;
Arrival curr2 = null;
if (iter1.hasNext()) {curr1 = iter1.next();}
if (iter2.hasNext()) {curr2 = iter2.next();}

while
((curr1 != null) && (curr2 != null))
{ if (curr1.compareWith(curr2) <= 0) // or <
{ result.addLast(curr1);
if (iter1.hasNext()) {curr1 = iter1.next();}
else {curr1 = null;}
}
else
{ result.addLast(curr2);
if (iter2.hasNext()) {curr2 = iter2.next();}
else {curr2 = null;}
}
}

while
(iter1.hasNext()) result.addLast(iter1.next());
while (iter2.hasNext()) result.addLast(iter2.next());
return result;
}
e.

Examiners report

[N/A]
a.
[N/A]
b.
[N/A]
c.
[N/A]
d.i.
[N/A]
d.ii.
[N/A]
e.

Syllabus sections

Option D: Object-oriented programming » D.4 Advanced program development
Show 33 related questions
Option D: Object-oriented programming

View options