IBDP Computer Science B2.2 Data structures SL Paper 2 - New Syllabus

Question 

All Car objects have been read into a large unsorted array called allCars.
A method is needed to show customers the range of available cars.
This method should take the array allCars as a parameter and select Car objects from allCars so that every available brandModel is presented only once.
You may assume that there are never more than 100 different types of cars (as identified by the variable brandModel).
(a) Define the term parameter variable.
(b) Construct the code for the method findBrandModels() that will take the array allCars as a parameter. It must return a Car array that contains every brandModel that is available without duplication.
A customer wants to see which different types of cars are available. The criteria are it must be a petrol car with automatic transmission and cost less than 35 euros per day.
(c) Without writing code, outline the steps needed for a method to perform this query and present the results to the customer.

Most-appropriate topic codes (IBDP Computer Science HL Paper 2):

• B2.2: Data structures – Parts (b), (c)
• B2.3: Programming constructs – Parts (b), (c)
• B2.4: Programming algorithms – Parts (b), (c)
• B3.2: Fundamentals of OOP for multiple classes [HL only] – Parts (b), (c)

▶️ Answer/Explanation

(a)

For the correct answer:
The value or variable passed when the function/method is called; passed as a value or as a reference; is found in the parameter list of the method definition/signature.

A parameter variable is a named variable in a method definition that receives the argument supplied when the method is called. In findBrandModels(allCars), the array reference is passed to the method through its parameter.

(b)

For the correct answer:

public Car[] findBrandModels(Car[] allCars) {
    Car[] result = new Car[100];
    int count = 0;

    for (int i = 0; i < allCars.length && allCars[i] != null; i++) {
        boolean found = false;

        for (int j = 0; j < count; j++) {
            if (result[j] != null &&
                result[j].getBrandModel().equals(allCars[i].getBrandModel())) {
                found = true;
                break;
            }
        }

        if (!found) {
            result[count] = allCars[i];
            count++;
        }
    }

    return result;
}

The method creates an array capable of holding the maximum of 100 different car types. The outer loop examines each Car in allCars. For every Car, the inner loop checks whether the same brandModel has already been placed in result. If it has not been found, the Car object is added to the result array. This ensures that each available brandModel occurs only once.

(c)

For the correct answer:

  1. Call findBrandModels() to obtain an array containing each available brandModel without duplication.
  2. Create a result array to store the cars satisfying the customer’s requirements.
  3. Iterate through the returned array, checking each non-null Car object.
  4. Check that the car is a petrol car.
  5. Check that the car has an automatic transmission.
  6. Check that the price per day is less than €35.
  7. If all three conditions are satisfied, add the Car object to the result array.
  8. Output or return the matching car objects, including their brandModel information, to the customer.

The filtering ensures that only distinct car types meeting all three criteria are presented. The original allCars array remains unchanged.

Scroll to Top