0% found this document useful (0 votes)
56 views

IB Computer Science - Object Oriented Programming Test 3 (1)

Uploaded by

Melih Gülcan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

IB Computer Science - Object Oriented Programming Test 3 (1)

Uploaded by

Melih Gülcan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

IB Computer Science - Object Oriented Programming Test 3

[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:

decreases the health variable of the defending monster by the amount


returned by the rollDice method
uses the output method to display a defending monster’s name, damage
and health points if its health variable is greater than zero
sets the defending monster’s health variable to zero if it is less than zero
outputs the defending monster’s name followed by “must leave the game”
if its health variable is zero.

(a) Construct the method attack(Monster defender), using any


existing static methods in the Monster class that might be
useful. [4]

The method addMonster(Monster M) does the following:

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.

(b) Construct the method addMonster(Monster M). [5]

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.

public class Flight


{ private String id;
public String getId() {return this.id;}
// ... more variables, accessor and mutator methods
}

public class Arrival


{ private Flight myFlight;
private String sta; // Scheduled Time of Arrival ("hh:mm")
private int runway;
private String gate;
private int delay;
private boolean landed;

public Arrival(Flight myFlight, String sta)


{ this.myFlight = myFlight;
this.sta = sta;
this.runway = 0;
this.gate = null;
this.delay = 0;
this.landed = false;
}

public void addDelay(int newDelay)


{ this.delay = newDelay;
}

public String getETA()


{ // calculates the Estimated Time of Arrival (ETA) of the flight
// by adding the delay to the sta and returning the result as a
// String ("hh:mm")
}
public int compareWith(String flightID)
{ if (myFlight.getID().equals(flightID)) { return 0; }
else { return 1; }
}

public int compareWith(Arrival anotherArrival)


{ // missing code
}

// ... plus accessor and mutator methods


}
(a.i) Construct a UML diagram to represent the Arrival object.
[4] 

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 class FlightManagement


{
private Arrival[] inbound; // array of inbound airplanes
private int last = -1; // index of last used entry

public FlightManagement()
{ inbound = new Arrival[200];
}

public void add(Arrival newArrival)


{ // missing code that adds the newArrival to the array inbound
// sorted by ETA, and updates last
}

private int search (String flightID)


{ // missing code that searches the array inbound and
// returns the index of the Arrival object with flightID
}

public Arrival remove(String flightID)


{ Arrival result;
int index = search(flightID);
result = inbound[index];
while (index < last)
{ inbound[index] = inbound[index + 1];
index++;
}
last--;
return result;
}

// ... many more methods


}

The method search in the FlightManagement class searches the array


inbound and returns the index of the Arrival object with the given flightID.
(a.ii) Construct the method search implementing a linear search. Use
the first compareWith() method in the Arrival object.

You may assume that an Arrival object with the given


flightID exists in the array inbound. [4] 

3. [Maximum mark: 15]


An Event has been instantiated with 2 qualifying heats for a total of 11
swimmers.

Event free100 = new Event("100 m free style",2);

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] 

4. [Maximum mark: 10]


The array inbound in the FlightManagement class is sorted by Estimated Time of
Arrival (ETA).

(a) Construct a method showDelayed() that outputs the IDs of all


delayed flights in the array inbound that have not yet landed
and that have an ETA before a given time t. The time t is passed
as a String parameter. [4] 

(b) Without using a sorting algorithm, construct the method


add(Arrival newArrival) that inserts a newArrival in the sorted
array inbound. You may assume that newArrival has been
instantiated and that the array inbound is not full. [6] 

© International Baccalaureate Organization, 2024

You might also like