2021 AP CSA FRQ 2: CombinedTable

2021 FRQ #2: CombinedTable

Class Design

9 Points
Timer
25:00

Problem Description

This question involves a class CombinedTable representing two SingleTable objects pushed together. A person can sit at a combined table if there is room for them at one of the single tables.

SingleTable Class (provided)

public class SingleTable
{
    /** Returns the number of seats */
    public int getNumSeats()
    { /* implementation not shown */ }

    /** Returns the height in inches */
    public int getHeight()
    { /* implementation not shown */ }

    /**
     * Returns the quality of the view from this table:
     * 1 = bad, 2 = okay, 3 = good
     */
    public int getViewQuality()
    { /* implementation not shown */ }

    /** Sets the number of seats */
    public void setNumSeats(int num)
    { /* implementation not shown */ }
}

When two tables are combined:

  • Total seats = sum of seats minus 2 (lose 2 seats at the joining point)
  • Tables can only be combined if they have the same height
  • The desirability is the average of the view qualities

CombinedTable Requirements

public class CombinedTable
{
    // Constructor: takes two SingleTable objects
    // canSeat(int n): returns true if n people can sit
    // getDesirability(): returns average view quality
}

Write the Complete Class

🔓 Solution
public class CombinedTable
{
    private SingleTable table1;
    private SingleTable table2;

    public CombinedTable(SingleTable t1, SingleTable t2)
    {
        table1 = t1;
        table2 = t2;
    }

    public boolean canSeat(int n)
    {
        return n <= table1.getNumSeats() + table2.getNumSeats() - 2;
    }

    public double getDesirability()
    {
        return (table1.getViewQuality() + table2.getViewQuality()) / 2.0;
    }
}

Related Practice: Class Design

Similar FRQs:

Study Guide:

Daily Practice:

Contact form