The Question

The question, from collegeboard, is shown below.

It's not showing Why

The Solution

Below is the function I contructed for the FRQ

public class CombinedTable { // class representing two combined SingleTable objects
    private SingleTable table1; // defining the two tables that will make up the CombinedTable object
    private SingleTable table2;

    public CombinedTable(SingleTable t1, SingleTable t2) { // constructor function for setting the variables
        table1 = t1; // sets the table1 and table2 variables to their respective SingleTable objects
        table2 = t2;
    }

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

    public double getDesirability() {
        double desirability = (table1.getViewQuality()+table2.getViewQuality())/2;
        if (table1.getHeight()!=table2.getHeight()) {
            desirability-=10;
        }
        return desirability;
    }
}

The Breakdown

The breakdown of the solution and the process by which I came up with it.

Initial variables

I initialized the variables, from the logs we can see that the values of CombinedTable changes when one of the table changes, thus, it’s better to make the variables dynamic and the outputs change based on the tables, which should be taken as instance variables.

public class CombinedTable { // class representing two combined SingleTable objects
    private SingleTable table1; // defining the two tables that will make up the CombinedTable object
    private SingleTable table2;
}

Init Function

The init function was simple, after all you just set up the two SingleTable variables, just have to make sure the typing is right.

public class CombinedTable { // class representing two combined SingleTable objects
    private SingleTable table1; // defining the two tables that will make up the CombinedTable object
    private SingleTable table2;

    public CombinedTable(SingleTable t1, SingleTable t2) { // constructor function for setting the variables
        table1 = t1; // sets the table1 and table2 variables to their respective SingleTable objects
        table2 = t2;
    }
}

The canSeat function

I was able to condense the logic on a single line, basically the function was asking is the number given is less than or equal to two less than the sum of the two SingleTable’s height, which I obtained through the use of the getNumSeats function on the variables stored.

public class CombinedTable { // class representing two combined SingleTable objects
    private SingleTable table1; // defining the two tables that will make up the CombinedTable object
    private SingleTable table2;

    public CombinedTable(SingleTable t1, SingleTable t2) { // constructor function for setting the variables
        table1 = t1; // sets the table1 and table2 variables to their respective SingleTable objects
        table2 = t2;
    }

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

The getDesirability function

The most complicated method here, my approach was first to calculate the sum of the view quality (using the getViewQuality method in SingleTable), then subtracting 10 if the heights (from getHeight method) are not matching. I could’ve fitted the method in one line, but that will make it hard to read.

public class CombinedTable { // class representing two combined SingleTable objects
    private SingleTable table1; // defining the two tables that will make up the CombinedTable object
    private SingleTable table2;

    public CombinedTable(SingleTable t1, SingleTable t2) { // constructor function for setting the variables
        table1 = t1; // sets the table1 and table2 variables to their respective SingleTable objects
        table2 = t2;
    }

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

    public double getDesirability() {
        double desirability = (table1.getViewQuality()+table2.getViewQuality())/2;
        if (table1.getHeight()!=table2.getHeight()) {
            desirability-=10;
        }
        return desirability;
    }
}

Testing

I test the solution with a custom made SingleTable function, all the logs output as intended.

public class SingleTable {
    private int numSeats; // instance variables shown
    private int height;
    private double viewQuality;

    /** Constuctor sets up internal variables */
    public SingleTable(int n, int h, double v) {
        numSeats = n;
        height = h;
        viewQuality = v;
    }

    /**Returns the number of seats at this table. The value is always greater than or equal to 5 */
    public int getNumSeats() {
        /* implementation shown*/
        return numSeats;
    }

    /** Returns the height of this table in centimeters. */
    public int getHeight() {
        /* implementation shown*/
        return height;
    }

    /** Returns the quality of the view from this table. */
    public double getViewQuality() {
        /* implementation shown*/
        return viewQuality;
    }

    /** Sets the quality of the view from this table to value*/ 
    public void setViewQuality(double value) {
        /* implementation shown*/
        viewQuality = value;
    }
}

public class CombinedTable { // class representing two combined SingleTable objects
    private SingleTable table1; // defining the two tables that will make up the CombinedTable object
    private SingleTable table2;

    public CombinedTable(SingleTable t1, SingleTable t2) { // constructor function for setting the variables
        table1 = t1; // sets the table1 and table2 variables to their respective SingleTable objects
        table2 = t2;
    }

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

    public double getDesirability() {
        double desirability = (table1.getViewQuality()+table2.getViewQuality())/2;
        if (table1.getHeight()!=table2.getHeight()) {
            desirability-=10;
        }
        return desirability;
    }

    public static void main(String[] args) {  
        SingleTable t1 = new SingleTable(4,74,60.0);
        SingleTable t2 = new SingleTable(8,74,70.0);
        SingleTable t3 = new SingleTable(12,76,75.0);
        CombinedTable c1 = new CombinedTable(t1, t2);
        System.out.println(c1.canSeat(9));
        System.out.println(c1.canSeat(11));
        System.out.println(c1.getDesirability());
        CombinedTable c2 = new CombinedTable(t2,t3);
        System.out.println(c2.canSeat(18));
        System.out.println(c2.getDesirability());
        t2.setViewQuality(80);
        System.out.println(c2.getDesirability());
    }
}

CombinedTable.main(null);
true
false
65.0
true
62.5
67.5

Points and Reflection

|Points|Description| |-|-| |1|I sucessfully declared CombinedTable and the constructor with SingleTable objects| |2|I declared 2 private SingleTable variables| |3|I initialized the t1 and t2 values in the Constructor| |4|I declared teh header successfully, including a int parameter.| |5|I used getNumSeats on a SingleTable object while determining the canSeat function| |6|I have the canSeat logic correctly setup.| |7|I correctly declares the getDesirability header without any parameters| |8|I used getHeight on t1 and t2 to get the height| |9|I gave the correct values for the different view qualities|

Points: 9/9

I could’ve improved by providing more comments on individual variables to make their meanings more clear, but I do think everything else is pretty clean.