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; } }
Aren’t you supposed to find the average view quality for the two tables? That means you got to divide by two.
Yes…
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?
I have not. I like it though.
Should we use “this” in the constructor and should the elements of the class be named the same as parameters (t1 & t2)?
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 thethis
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 thethis
keyword, you would likely be better off just naming them differently.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?
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 theCombinedTable
must reflect the update.thanks for detailed explanation. i was able to explain to my kid.