CombinedTable free response 9

CombinedTable free response problem from the 2021 AP Computer Science A Exam.

CombinedTable is #2 from the from the 2021 AP Computer Science A Free Response problems.

https://apcentral.collegeboard.org/pdf/ap21-frq-computer-science-a.pdf?course=ap-computer-science-a

Part (a) – CombinedTable class

public class CombinedTable
{
  private SingleTable t1, t2;

  public CombinedTable(SingleTable t1, SingleTable t2)
  {
    this.t1 = t1;
    this.t2 = t2;
  }
	
  public boolean canSeat(int people)
  {
    int seats = t1.getNumSeats() + t2.getNumSeats() - 2;
    return seats >= people;
  }
	
  public double getDesirability()
  {
    double averageView = (t1.getViewQuality() + t2.getViewQuality()) / 2;

    if(t1.getHeight() == t2.getHeight())
       return averageView;
    else
      return averageView - 10;
  }
}

2021 AP CS Exam Free Response Solutions

9 thoughts on “CombinedTable free response

  1. Miles May 10,2021 1:31 am

    Aren’t you supposed to find the average view quality for the two tables? That means you got to divide by two.

  2. Brian May 10,2021 12:20 pm

    one thing I thought while writing this solution is that since SingleTable objects are mutable (setViewQuality) the getDesiarbility method needs to determine the desirability of the CombinedTable (like your solution clearly shows). It cannot be solely assigned in the constructor. Have you seen this issue emphasized on any previous FRQs?

  3. Serkan May 15,2021 9:23 am

    Should we use “this” in the constructor and should the elements of the class be named the same as parameters (t1 & t2)?

    • Brandon Horn May 16,2021 9:46 am

      If you name the instance variables and the parameters the same, you must use the this keyword to distinguish between them. If you name them differently, you don’t need the this keyword. I like to name them the same to prevent accidentally referring to the constructor parameters instead of the instance variables under time constraints. If you are uncomfortable with the this keyword, you would likely be better off just naming them differently.

  4. Will May 17,2021 2:26 pm

    I wonder if you do not declare Single table in the data field, instead you declare the int number of totalSeat and double value of totalDesirability. Then you do the calculation in constructor. For the methods, you only compare the parameter in canSeat to the number of totalSeat, and you return the number of totalDesirability in getDesirability. Would that be alright?

    • Brandon Horn May 18,2021 8:10 am

      No, you must store references to the SingleTable objects passed to the constructor. The example code shows the view quality of one of the component tables modified and explicitly describes that the view quality of the CombinedTable must reflect the update.

  5. djearam May 18,2021 7:20 am

    thanks for detailed explanation. i was able to explain to my kid.

Comments are closed.