Other 2014 FR Questions FR other years Be Prepared Home
A-3
Part (a)
  public SeatingChart(List<Student> studentList, int rows, int cols)
  {
    seats = new Student[rows][cols]; 1

    int r = 0, c = 0;
    for (Student s : studentList)
    {
      seats[r][c] = s;
      r++;
      if (r == rows)
      {
        r = 0;
        c++;
      }
    }
  } 2
Notes:
  1. When an array of objects is declared, all elements are set to null by default. This fact is not in the AP subset, but knowing it saves a couple of lines of code.

  2. Alternative solution:
      public SeatingChart(List<Student> studentList, int rows, int cols)
      {
        seats = new Student[rows][cols];
        for (int i = 0; i < studentList.size(); i++)
          seats[i % rows][i / rows] = studentList.get(i);
      }
    


Part (b)
  public int removeAbsentStudents(int allowedAbsences)
  {
    int removedCount = 0; 
    for (int r = 0; r < seats.length; r++)
    {
      for (int c = 0; c < seats[0].length; c++)
      {
        if (seats[r][c] != null && seats[r][c].getAbsenceCount() > allowedAbsences) 1
        {
          seats[r][c] = null;
          removedCount++;
        }
      }
    }
    return removedCount;
  }
Notes:
  1. It is important to check first that the value seats[r][c] is not null. Failure to do so may result in NullPointerException when getAbsenceCount is called.

Other 2014 FR Questions | Back to Contents

Copyright © 2014 by Skylight Publishing
support@skylit.com