IBDP Computer Science B2.2 Data structures SL Paper 2 - New Syllabus
Question
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).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.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:
- Call
findBrandModels()to obtain an array containing each availablebrandModelwithout duplication. - Create a result array to store the cars satisfying the customer’s requirements.
- Iterate through the returned array, checking each non-null Car object.
- Check that the car is a petrol car.
- Check that the car has an automatic transmission.
- Check that the price per day is less than €35.
- If all three conditions are satisfied, add the Car object to the result array.
- Output or return the matching car objects, including their
brandModelinformation, to the customer.
The filtering ensures that only distinct car types meeting all three criteria are presented. The original allCars array remains unchanged.
