Combined Table Free Response Solution

The Combined Table free response question from the 2021 AP Computer Science exam has you working to create a class from scratch, and it’s a class that you haven’t seen before. And, it also has you using another new class as instance variables. So it’s testing two things. One, that you know the class structure in Java well enough to build one entirely from scratch. And two, that you understand composition, specifically when one class is composed of another class.

If you haven’t already, it’s worth taking a look at the full PDF for this problem. We’ll look at snippets here, but aren’t going to bring over the entire thing.

Single Table

First, let’s look at the SingleTable class. It’s given to you, and there’s nothing to do in this class. But since we’ll be using it later, we do need to understand what’s here.

public class SingleTable {
	public int getNumSeats() {
		// implementation not shown
	}
	public int getHeight() {
		// implementation not shown
	}
	public double getViewQuality() {
		// implementation not shown
	}
	public void setViewQuality(double value) {
		// implementation not shown
	}
	// Other instance variables, constructor and
	// methods not shown
}

First, notice that all of the methods are // implementation not shown. That means that we’re probably going to call these methods, but don’t have to care how they work. They just work.

Combined Table

The class you’re writing, CombinedTable is used to wrap 2 SingleTable instances and determine the overall desirability of the pair of tables with the following rules

  • A CombinedTable has two fewer seats than the two SingleTable that it’s composed of
  • If the two SingleTable are the same height, the CombinedTable desirability is the average of the view qualities
  • If they’re not the same height, it’s 10 less than the average

Class structure and constructors

Using the sample method calls given to us on the PDF, we know what the constructor needs to look like, and we can extrapolate from that any needed instance variables.

We’re given this as an example, where t1 and t2 are existing SingleTable references.

CombinedTable c1 = new CombinedTable(t1, t2);

With that we can build the first part of the class.

public class CombinedTable {
	private SingleTable tableOne;
	private SingleTable tableTwo;
	public CombinedTable(SingleTable t1, SingleTable t2) {
		tableOne = t1;
		tableTwo = t2;
	}
}

We’re not done yet, but this is a start.

The class header is pretty standard for all classes in AP Computer Science. public followed by the keyword class, followed by the name of the class. There’s no inheritance in this problem, so we go ahead and open the braces.

Since the constructor has two SingleTable references as a parameter, it’s a good hint that you’re going to need to instance variables to hold them. And since instance variables are always private in AP CompSci FRQ, we made them private here.

And then it’s just up to the constructor to take the parameters and store them in the instance variables.

canSeat Method

canSeat is a boolean return method that takes an integer and determines if the two tables combined can seat that many people.

public boolean canSeat(int howMany) {
	int totalAvailable = tableOne.getNumSeats() + tableTwo.getNumSeats() - 2;
	return totalAvailable >= howMany;
}

What we’re doing is getting the total seats between the two tables and subtracting two, storing that it the variable totalAvailble. That’s then compared to howMany to get the return value;

getDesirability Method

Now we get to decide how good a combined table is, and this is calculated by using the view quality of the two single tables using the rules listed above.

public double getDesirability() {
	double avg = (tableOne.getViewQuality() + tableTwo.getViewQuality()) / 2.0;
	if (tableOne.getHeight() == tableTwo.getHeight()) {
		return averageView;
	} else {
		return averageView - 10;
	}
}

We get the average of the two view qualities. If the tables are the same height, we just return that average. If they’re not, we subtract to to what we return.

You might also do something like this and end up with the same result.

public double getDesirability() {
	double avg = (tableOne.getViewQuality() + tableTwo.getViewQuality()) / 2.0;
	if (tableOne.getHeight() != tableTwo.getHeight()) {
		avg -= 10;
	}
	return avg;
}

Same end result. Just another way to get there.

Complete CombinedTable class

And we’re done. Just so it’s all in one spot, here’s the complete CombinedTable class.

public class CombinedTable {
	private SingleTable tableOne;
	private SingleTable tableTwo;
	public CombinedTable(SingleTable t1, SingleTable t2) {
		tableOne = t1;
		tableTwo = t2;
	}

	public boolean canSeat(int howMany) {
		int totalAvailable = tableOne.getNumSeats() + tableTwo.getNumSeats() - 2;
		return totalAvailable >= howMany;
	}

	public double getDesirability() {
		double avg = (tableOne.getViewQuality() + tableTwo.getViewQuality()) / 2.0;
		if (tableOne.getHeight() == tableTwo.getHeight()) {
			return averageView;
		} else {
			return averageView - 10;
		}
	}
}
This site contains affiliate links. If you click an affiliate link and make a purchase we may get a small commission. It doesn't affect the price you pay, but it is something we must disclose.