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

Question 

A lawyer’s rank depends on the number of cases they have won.

The information about the number of cases won by each lawyer in the last four years is stored in a 2D array, casesWon, as shown in Figure 11. Rows represent the number of cases won by a lawyer, and columns represent the year.

Figure 11: An excerpt from the 2D array, casesWon

A lawyer’s rank is set to 1 only if the number of cases won by the lawyer in any two consecutive years is greater than 6.

For example: the rank for lawyer 1 will remain 0, whereas the rank for lawyer 2 will be set to 1 as the number of cases they won is greater than 6 in both 2022 and 2021.

(a) Construct the method updateLawyerRank(int[][] casesWon) to update lawyerRank for all the lawyers in the firm. The method should output a suitable message if no lawyer’s rank changes to 1. [7]

The law firm can have more than one criminal lawyer. A criminal lawyer gets only criminal cases.

(b) Identify the steps required to create an ArrayList of criminal cases that the law firm currently has and store all criminal cases to this array list. [3]

Most-appropriate topic code

B2.3: Programming constructs — part (a)
B2.2: Data structures — part (b)
▶️ Answer/Explanation

(a)
Award [7 max]

The method should use a flag to determine whether at least one lawyer’s rank has been changed. It must compare each pair of consecutive years and set the lawyer’s rank to 1 if both values are greater than 6.

public static void updateLawyerRank(int[][] casesWon)
{
    boolean flag = false;

    for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if(casesWon[i][j] > 6 && casesWon[i][j + 1] > 6)
            {
                allLawyers[i].setLawyerRank(1);
                flag = true;
            }
        }
    }

    if(flag == false)
    {
        System.out.println("No lawyer rank was changed to 1");
    }
}

The inner loop stops at j < 3 because j + 1 must remain within the four year columns.

(b)
Award [3 max]

  1. Declare an ArrayList of Case objects, for example criminalCases, to store all criminal cases.
  2. Loop through the allLawyers[] array from the first lawyer to the last lawyer.
  3. For each lawyer whose lawyerType is "criminal", loop through their lawyerCases[] array.
  4. Check each element for null before accessing the case.
  5. Add each valid criminal case to the ArrayList.
ArrayList<Case> criminalCases = new ArrayList<Case>();

for(int i = 0; i < allLawyers.length; i++)
{
    if(allLawyers[i].getLawyerType().equals("criminal"))
    {
        for(int j = 0; j < allLawyers[i].getLawyerCases().length; j++)
        {
            if(allLawyers[i].getLawyerCases()[j] != null)
            {
                criminalCases.add(allLawyers[i].getLawyerCases()[j]);
            }
        }
    }
}
Scroll to Top