IB DP Computer Science Option D: Object-oriented programming
Sample Paper 1

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.
Flight
Each Flight has many attributes including a unique ID code.
Arrival
In addition to a variable of the type Flight, each Arrival has a Scheduled Time of Arrival (STA) in 24-hour format, a runway number, a gate code, a delay time (in minutes) and an indicator whether the airplane has landed.
FlightManagement
The main class that manages the arrival and departure of all flights.
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) Outline the general nature of an object.
(ii) Describe two disadvantages of using Object Oriented Programming (OOP).
(iii) Outline one advantage of using modularity in program development.
b. State the relationship between the Flight object and the Arrival object.
c. Construct a UML diagram to represent the Arrival object.
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.
d. 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.
e. (i) Outline one advantage of using a binary search.
(ii) Outline one disadvantage of using a binary search.
2. a. Outline the use of data-hiding as a security feature in OOP.
b. (i) The Arrival object has two methods named compareWith.
Outline why calling the compareWith method does not cause a conflict.
(ii) State the name of this OOP property.
The method compareWith(Arrival anotherArrival) compares the Estimated Time of Arrival (ETA) of the current Arrival object with the ETA of another Arrival object. The method should return a negative value if the current Arrival object’s ETA is less than the ETA of anotherArrival.
Remember that the String class contains a method compareTo() as follows: given two String variables A and B,
A.compareTo(B) < 0 when A is lexicographically smaller than B A.compareTo(B) = 0 when A is equal to B
A.compareTo(B) > 0 when A is lexicographically bigger than B.
c. Construct the code for this method compareWith().
3. a. Define the term method signature.
The array inbound in the FlightManagement class is sorted by Estimated Time of Arrival (ETA).
b. 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.
c. 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. When a flight is delayed, a method in FlightManagement is used to find and update the flight with the delay and to reorganize the array so that it remains sorted for the ETA.
d. Describe how this method would be implemented using the methods provided.
- a. (i)
An object in programming encapsulates data and behavior into a single unit. It has attributes (data) and methods (behavior) that operate on that data. Objects interact with each other through methods, and they can be created, modified, and destroyed during program execution.
(ii) Two disadvantages of using Object-Oriented Programming (OOP) are:
- Overhead: OOP can introduce additional complexity and overhead, especially in large-scale systems. Object creation, method dispatching, and memory management can incur performance costs compared to procedural or functional programming paradigms.
- Complexity: OOP encourages abstraction and encapsulation, which can lead to complex class hierarchies and relationships. Understanding and maintaining such systems can be challenging, especially for new developers or when the design is not well-structured.
(iii) One advantage of using modularity in program development is that it promotes code reuse and simplifies maintenance. By breaking down a program into smaller, modular components, each responsible for a specific task or functionality, developers can easily identify, debug, and update individual modules without affecting the entire system. This enhances flexibility, scalability, and collaboration in software development.
b. The relationship between the Flight object and the Arrival object is that each Arrival object has a reference to a Flight object. This relationship implies that an Arrival is associated with a particular Flight, indicating that the Flight is arriving at a specific destination.
c. UML diagram representing the Arrival object:
___________________________________________
| Arrival |
|__________________________________________|
| - myFlight: Flight |
| - sta: String |
| - runway: int |
| - gate: String |
| - delay: int |
| - landed: boolean |
|__________________________________________|
| + Arrival(myFlight: Flight, sta: String) |
| + addDelay(newDelay: int) |
| + getETA(): String |
| + compareWith(flightID: String): int |
| + compareWith(anotherArrival: Arrival): int |
|__________________________________________
d. Here’s the implementation of the search() method using linear search
private int search(String flightID) {
for (int i = 0; i <= last; i++) {
if (inbound[i].compareWith(flightID) == 0) {
return i;
}
}
// Assume the object exists, so this line won't be reached.
return -1;
}
e.
(i) One advantage of using a binary search is its efficiency in large sorted datasets. It has a time complexity of O(log n), making it significantly faster than linear search, especially when dealing with a large number of elements. Binary search can quickly narrow down the search space by repeatedly dividing the dataset in half.
(ii) One disadvantage of using a binary search is that it requires the dataset to be sorted beforehand. Sorting the data adds an extra computational overhead, especially if the dataset is frequently updated or if the sorting algorithm used is not efficient. Additionally, binary search is not suitable for unsorted or dynamically changing datasets, as maintaining the sorted order becomes impractical or inefficient.
2. (a) Data-hiding in OOP refers to the practice of restricting access to certain data within a class, allowing only authorized methods to modify or view that data. By encapsulating data and providing controlled access through methods (getters and setters), data-hiding helps ensure the integrity and security of the object’s internal state. This prevents unauthorized modification or manipulation of data, reducing the risk of bugs, errors, and security vulnerabilities.
(b) (i) Calling the compareWith method does not cause a conflict because the two methods have different parameter lists. One method compares the Arrival object with a String flightID, while the other compares the Arrival object with another Arrival object. Java allows method overloading, where multiple methods can have the same name but different parameter lists. The method to be invoked is determined based on the number and types of arguments provided in the method call.
(ii) This OOP property is called method overloading.
public int compareWith(Arrival anotherArrival) {
// Extract ETA from strings and compare lexicographically
if (this.getETA().compareTo(anotherArrival.getETA())>0) {
return 1;
}
else {
return -1;
}
}
3. (a) Method signature refers to the unique combination of a method’s name and parameter list. It includes the method’s name along with the number, types, and order of its parameters. The return type is not considered part of the method signature.
(b) Method showDelayed() to output IDs of delayed flights:
public void showDelayed(String t) {
for (Arrival arrival:inbound) {
if (arrival!=null && !arrival.haslanded()&& arrival.getdelay()>0 && arrival.getETA().compareTo(t)<0){
System.out.println("delayed flight id "+arrival.getFlight().getId());
}
}
}
C. Method add(Arrival newArrival) to insert newArrival into the sorted array inbound:
public void add(Arrival newArrival) {
int index = last;
while (index >= 0 && newArrival.getETA().compareTo(inbound[index].getETA()) < 0) {
inbound[index + 1] = inbound[index];
index--;
}
inbound[index + 1] = newArrival;
last++;
}
(d) To implement the method for updating flight delays and reorganizing the array, you would follow these steps:
- Find the index of the Arrival object in the array using the search() method, passing the Flight ID or ETA.
- Once found, update the delay time for the corresponding Arrival object.
- If the delay affects the order of the array, you would need to reorganize it. Since the array is sorted by ETA, you would need to move the updated Arrival object to its correct position in the array to maintain the sorted order.
- After updating the delay and reorganizing the array, the FlightManagement class should be able to handle further operations efficiently, maintaining the sorted order of the array based on ETA.
Find the complete code:
package sample;
public class Flight
{ private String id;
public Flight(String id) {
this.id=id;
}
public String getId() {
return this.id;
}
// ... more variables, accessor and mutator methods
}
package sample;
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")
String[] s=sta.split(":");
int hour=Integer.parseInt(s[0])+delay;
String time=String.valueOf(hour)+s[1];
return String.valueOf(time);
}
public int compareWith(String flightID)
{
if (myFlight.getId().equals(flightID))
{
return 0;
}
else { return 1; }
}
public int compareWith(Arrival anotherArrival)
{ // missing code
if (this.getETA().compareTo(anotherArrival.getETA())>0) {
return 1;
}
else {
return -1;
}
}
public Flight getFlight() {
return myFlight;
}
// ... plus accessor and mutator methods
public boolean haslanded() {
// TODO Auto-generated method stub
return landed;
}
public int getdelay() {
return delay;
}
}
package sample;
import java.util.Arrays;
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) {
int index = last;
while (index >= 0 && newArrival.getETA().compareTo(inbound[index].getETA()) < 0) {
inbound[index + 1] = inbound[index];
index--;
}
inbound[index + 1] = newArrival;
last++;
}
public void display() {
System.out.println("hi");
for (Arrival arrival : inbound) {
if (arrival!=null) {
System.out.println(arrival.getFlight().getId()+" "+arrival.getETA());
}
}
}
private int search(String flightID) {
for (int i = 0; i <= last; i++) {
if (inbound[i].compareWith(flightID) == 0) {
return i;
}
}
return -1; // If not found
}
public void showdelayed(String t) {
for (Arrival arrival:inbound) {
if (arrival!=null && !arrival.haslanded()&& arrival.getdelay()>0 && arrival.getETA().compareTo(t)<0){
System.out.println("delayed flight id "+arrival.getFlight().getId());
}
}
}
public static void main(String[] args) {
// Create FlightManagement object
FlightManagement flightManager = new FlightManagement();
// Create Flight objects
Flight flight1 = new Flight("F001");
Flight flight2 = new Flight("F002");
Flight flight3=new Flight("F003");
// Create Arrival objects
Arrival arrival1 = new Arrival(flight1, "10:00");
Arrival arrival2 = new Arrival(flight2, "11:30");
Arrival arrival3 = new Arrival(flight3,"09:00");
//Add delay
arrival1.addDelay(2);
arrival2.addDelay(1);
arrival3.addDelay(3);
System.out.println(arrival1.getETA());
System.out.println(arrival2.getETA());
// Add arrivals to flight management
flightManager.add(arrival1);
flightManager.add(arrival2);
flightManager.add(arrival3);
flightManager.display();
flightManager.showdelayed("20");
// Other methods
}}