Java

OOP GUIDE / WORK

 

COMBINEDTABLE CLASS

 

Topics

  • Advanced class design
  • Aggregation

 

Source

  • This question was part of the 2021 AP exam free response questions.  A link to all past AP exam free response questions is available on the index page.

 

 

TASK – PART 1 – SETUP - SINGLETABLE CLASS

 

Copy and paste the following SingleTable class to your IDE.

 

 

public class SingleTable

{

     private int seats;

     private double viewQuality;     //0.0 to 100.0

     private int height;             //in cm

    

     /** Constructor */

 

     public SingleTable(int s, double vq, int h)

     {

           seats = s;

           viewQuality = vq;

           height = h;

     }

    

     /** Returns the number of seats at this table. The value is always greater than or equal to 4. */

 

     public int getNumSeats()

     {

           return seats;

     }

    

     /** Returns the height of this table in centimeters. */

 

     public int getHeight()

     {

           return height;

     }

    

     /** Returns the quality of the view from this table. */

 

     public double getViewQuality()

     {

           return viewQuality;

     }

    

     /** Sets the quality of the view from this table to value. */

 

     public void setViewQuality(double value)

     {

           viewQuality = value;

     }   

}

 

 

 

TASK – PART 2 – CREATING THE COMBINEDTABLE CLASS

 

Click here for a PDF containing the specification of the CombinedTable class that you must implement.

 

 

TASK – PART 3 – TESTING THE COMBINEDTABLE CLASS

 

Copy and paste the following code that will test your CombinedTable class.  Note that the program below matches the example method calls in the original question.  You should compare your output to the examples to see if they match.

 

 

public class CombinedTableTester

{

     public static void main(String[] args)

     {

           SingleTable t1 = new SingleTable(4, 60.0, 74);

           SingleTable t2 = new SingleTable(8, 70.0, 74);

           SingleTable t3 = new SingleTable(12, 75.0, 76);

          

           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());

     }

}