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

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;
            return averageView - 10;

See Class writing order for a technique to respond to AP CS FR that request an entire class.

The problem explicitly states, “… when the characteristics of a SingleTable change, so do those of the CombinedTable that contains it.”

CombinedTable must store a reference to each SingleTable object. Storing the number of seats and/or the desirability of the CombinedTable as instance variables is a mistake.

It is possible to store the number of seats and/or the desirability as instance variables in CombinedTable without incurring a penalty (if the variables are updated immediately prior to use in canSeat and/or getDesirability); however, it is still a mistake. See Duplicate data as instance variables for additional discussion.

2021 AP CS Exam Free Response Solutions

Additional classes & objects resources

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on CombinedTable