IB Computer Science - Object Oriented Programming Test 3 (1)
IB Computer Science - Object Oriented Programming Test 3 (1)
[42 marks]
1. [Maximum mark: 9]
The Monster class contains the method attack(Monster defender). This is used
when a monster attacks another monster (the defender).
This method:
If the monster array is not full and M is not in the array, it adds M to the array
andincreases the variable monsterCount.
Otherwise, a message is output stating the reason why.
2. [Maximum mark: 8]
An airport uses an object-oriented program to keep track of arrivals and
departures of planes. There are many objects in this system and some are listed
below.
The code below outlines the Arrival class used in this program.
The code below outlines part of the FlightManagement class used in this
program.
For the purposes of this exam only arriving flights are being considered.
public FlightManagement()
{ inbound = new Arrival[200];
}
The swimmers were added to the two Race arrays and after the races, their times
were recorded as shown in the table.
(For the purpose of this question, the name represents the full swimmer object.)
The method fillFinals() will select the 8 fastest swimmers, in ascending order of
time, from both swimmer arrays and copy them to the swimmer array in the
finals race.
(a) Sketch the resulting swimmer array in finals.
[3]
To help with this selection, all entries from races[0] and races[1] will be copied
into two new parallel arrays of size 16, one array for swimmers and one array for
their times.
(b) Construct the code fragment for the given situation that will
copy swimmers and times into two parallel arrays named
tempSwimmer and tempTime.
[6]
The two temporary arrays will be sorted using the following code.
int i,j;
Swimmer swapSwimmer;
double swapTime;
for(i = 0; i < 15; i++)
{
for(j = 0; j < 15; j++)
{
if(tempTime[j] > tempTime[j + 1]) // if wrong order then…
{
swapSwimmer = tempSwimmer[j]; // swap the swimmer and…
tempSwimmer[j] = tempSwimmer[j + 1];
tempSwimmer[j + 1] = swapSwimmer;
swapTime = tempTime[j]; // swap the time
tempTime[j] = tempTime[j + 1];
tempTime[j + 1] = swapTime;
}
}
}
(c) Construct the code fragment that will copy the names of the 8
fastest swimmers in ascending order of time from the array
tempSwimmer to the array swimmers in the race finals.
[6]