Skip to the content.

Classes FRQ Homework

public class CombinedTabel {
    // Declare two private instance variables to hold references to the SingleTable objects
    private SingleTable table1;
    private SingleTable table2;

    // Constructor to initialize the two SingleTable objects
    public CombinedTable(SingleTable table1, SingleTable table2) {
        this.table1 = table1;
        this.table2 = table2;
    }

    // Method to check if the combined tables can seat the specified number of people
    public boolean canSeat(int num) {
        // Get the total number of seats from both tables, subtracting 2
        int numOfSeats = table1.getNumSeats() + table2.getNumSeats() - 2;
        // Return true if the combined seats can accommodate the specified number of people, otherwise false
        if (numOfSeats >= num) {
            return true;
        } else {
            return false;
        }
    }

    // Method to calculate the desirability of the combined tables based on their view qualities
    public int getDesirability() {
        // Check if both tables have the same height
        if (table1.getHeight() == table2.getHeight()) {
            // If heights are equal, compute the average view quality
            int returnValue = (table1.getViewQuality() + table2.getViewQuality()) / 2;
            return returnValue;
        } else {
            // If heights are not equal, compute the average view quality and subtract 10
            int returnValue = ((table1.getViewQuality() + table2.getViewQuality()) / 2) - 10;
            return returnValue;
        }
    }
}

Scoring Guidelines

Declares class header:

  • Class header is correctly declared as public class CombinedTable

Point: ✅ (1 point)

Declares appropriate private instance variables:

  • I have correctly marked table1 and table2 as private.

Point: ✅ (1 point)

Constructor initializes instance variables using parameters:

  • The constructor correctly initializes the instance variables.

Point: ✅ (1 point)

Declares header for canSeat method:

  • The header is correctly declared as public boolean canSeat(int num).

Point: ✅ (1 point)

Calls getNumSeats on a SingleTable object:

  • I correctly call getNumSeats() on both table1 and table2.

Point: ✅ (1 point)

canSeat(n) returns true if and only if sum of seats of two tables - 2 >= n:

  • The condition in canSeat() correctly checks if the sum of seats (with 2 subtracted) is greater than or equal to num.

Point: ✅ (1 point)

Declares header for getDesirability method:

  • The header is correctly declared as public int getDesirability().

Point: ✅ (1 point)

Calls getHeight and getViewQuality on SingleTable objects:

  • I correctly call getHeight() and getViewQuality() on table1 and table2.

Point: ✅ (1 point)

getDesirability computes average of constituent tables’ view desirabilities:

  • The logic to compute the average of getViewQuality() is correct.

Point: ✅ (1 point)

Summary of Scoring:

  • Point 1: ✅
  • Point 2: ✅
  • Point 3: ✅
  • Point 4: ✅
  • Point 5: ✅
  • Point 6: ✅
  • Point 7: ✅
  • Point 8: ✅
  • Point 9: ✅

Total Points: 9/9

Reflection

This question was pretty straightforward. The only thing that I had to be careful about was the logic in the canSeat method. I had to make sure that I was subtracting 2 from the sum of the seats of the two tables before comparing it with the number of people that needed to be seated. The also kind of tricky bit was calculating the average of the view qualities of the two tables. I had to make sure that I was dividing by 2 and for when it wasn’t the same height, I had make sure I remebered to subtract 10. Overall, this question was pretty easy.

Extra Hack

The player will be given two tables and needs to determine if the tables can seat a given number of guests. If the tables can seat the guests, the player gets points based on the desirability of the combined tables (calculated from the view quality and height of the tables).

import java.util.Scanner;

public class TableSeaterGame {

    public static void main(String[] args) {
        // Initialize two tables with mock data
        SingleTable table1 = new SingleTable(4, 10, 8);  // 4 seats, height 10, view quality 8
        SingleTable table2 = new SingleTable(6, 12, 7);  // 6 seats, height 12, view quality 7
        
        // Create a CombinedTable object
        CombinedTabel combinedTable = new CombinedTabel(table1, table2);

        // Start the game
        Scanner scanner = new Scanner(System.in);
        
        // Ask the player how many people they need to seat
        System.out.println("Welcome to the Table Seater Game!");
        System.out.print("How many people do you need to seat? ");
        int numGuests = scanner.nextInt();
        
        // Check if the tables can seat the guests
        if (combinedTable.canSeat(numGuests)) {
            System.out.println("Great! The tables can seat " + numGuests + " guests.");
            
            // Calculate the desirability of the combined tables
            int desirability = combinedTable.getDesirability();
            System.out.println("The desirability of the combined tables is: " + desirability);
            
            // If the desirability is above a threshold, player wins a bonus
            if (desirability >= 10) {
                System.out.println("Bonus: You selected tables with a great view!");
            } else {
                System.out.println("It's not the best view, but it will do.");
            }
        } else {
            System.out.println("Oops! These tables cannot seat " + numGuests + " guests.");
        }
        
        scanner.close();
    }
}

// SingleTable class to simulate the properties of a table
class SingleTable {
    private int numOfSeats;
    private int height;
    private int viewQuality;

    public SingleTable(int numOfSeats, int height, int viewQuality) {
        this.numOfSeats = numOfSeats;
        this.height = height;
        this.viewQuality = viewQuality;
    }

    public int getNumSeats() {
        return numOfSeats;
    }

    public int getHeight() {
        return height;
    }

    public int getViewQuality() {
        return viewQuality;
    }
}

// CombinedTabel class to combine two tables and calculate seating and desirability
class CombinedTabel {
    private SingleTable table1;
    private SingleTable table2;

    public CombinedTabel(SingleTable table1, SingleTable table2) {
        this.table1 = table1;
        this.table2 = table2;
    }

    // Checks if the combined tables can seat the required number of guests
    public boolean canSeat(int num) {
        int numOfSeats = table1.getNumSeats() + table2.getNumSeats() - 2;
        return numOfSeats >= num;
    }

    // Calculates the desirability of the combined tables based on their height and view quality
    public int getDesirability() {
        if (table1.getHeight() == table2.getHeight()) {
            return (table1.getViewQuality() + table2.getViewQuality()) / 2;
        } else {
            return ((table1.getViewQuality() + table2.getViewQuality()) / 2) - 10;
        }
    }
}
TableSeaterGame.main(null);
Welcome to the Table Seater Game!
How many people do you need to seat? Great! The tables can seat 4 guests.
The desirability of the combined tables is: -3
It's not the best view, but it will do.