Skip to main content
Logo image

Section 15.9 FRQ 2 - Class Design - Part 3

From the 2025 Course and Exam Description:
Free-Response Question 2: Class Design. Students will be instructed to design and implement a class based on provided specifications and examples. A second class might also be included. Students will be provided with a scenario and specifications in the form of a table demonstrating ways to interact with the class and the results. The class must include a class header, instance variables, a constructor, a method, and implementation of the constructor and required method.
FRQ 2 requires the following skills:
  • Create a class header
  • Determine what private instance variables are needed by the class
  • Write a constructor with appropriate parameters
  • Implement a method according to the behavior specified in the table
Teacher Insights
  • You do not need to include import statements. You can assume that any standard Java class is already imported.
  • You will NEVER print. If you think you need to print, you probably need to return something.
  • You will NOT need to write a main. In this question, they are giving you the main.

Subsection 15.9.1 2015 FRQ 2 - HiddenWord

The following is a free response question from 2015. It was question 2 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
Question 2. Consider a guessing game in which a player tries to guess a hidden word. The hidden word contains only capital letters and has a length known to the player. A guess contains only capital letters and has the same length as the hidden word.
After a guess is made, the player is given a hint that is based on a comparison between the hidden word and the guess. Each position in the hint contains a character that corresponds to the letter in the same position in the guess. The following rules determine the characters that appear in the hint.
Figure 15.9.1. The hints
The HiddenWord class will be used to represent the hidden word in the game. The hidden word is passed to the constructor. The class contains a method, getHint that takes a guess and produces a hint.
For example, suppose the variable puzzle is declared as follows.
HiddenWord puzzle = new HiddenWord("HARPS");
The following table shows several guesses and the hints that would be produced.
Figure 15.9.2. Example guesses and hints

Activity 15.9.1.

Write the complete HiddenWord class, including any necessary instance variables, its constructor, and the method, getHint, described above. You may assume that the length of the guess is the same as the length of the hidden word.

Subsection 15.9.2 FRQ 2 - Class Design with Multiple Classes

Sometimes FRQs use multiple classes. They usually provide a class, then include objects related to that class as private instance variables in the next class. You have done several examples where the instance variables are Strings. A class with instance variables that are elements of another class is the same, but you will not know how the other class has been defined.
The following FRQ demonstrates this.

Subsection 15.9.3 2021 FRQ 2 - Combined Table

The class SingleTable represents a table at a restaurant.
public class SingleTable {
    /**
     * Returns the number of seats at this table. The value is always greater than or equal to 4.
     */
    public int getNumSeats() {
        /* implementation not shown */
    }

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

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

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

    // There may be instance variables, constructors, and methods that are not shown.
}
At the restaurant, customers can sit at tables that are composed of two single tables pushed together. You will write a class CombinedTable to represent the result of combining two SingleTable objects, based on the following rules and the examples in the chart that follows.
  • A CombinedTable can seat a number of customers that is two fewer than the total number of seats in its two SingleTable objects (to account for seats lost when the tables are pushed together).
  • A CombinedTable has a desirability that depends on the views and heights of the two single tables. If the two single tables of a CombinedTable object are the same height, the desirability of the CombinedTable object is the average of the view qualities of the two single tables.
  • If the two single tables of a CombinedTable object are not the same height, the desirability of the CombinedTable object is 10 units less than the average of the view qualities of the two single tables.
Assume SingleTable objects t1, t2, and t3 have been created as follows.
  • SingleTable t1 has 4 seats, a view quality of 60.0, and a height of 74 centimeters.
  • SingleTable t2 has 8 seats, a view quality of 70.0, and a height of 74 centimeters.
  • SingleTable t3 has 12 seats, a view quality of 75.0, and a height of 76 centimeters.
The chart contains a sample code execution sequence and the corresponding results.
Figure 15.9.3.
The last line of the chart illustrates that when the characteristics of a SingleTable change, so do those of the CombinedTable that contains it.

Activity 15.9.2.

Write the complete CombinedTable class. Your implementation must meet all specifications and conform to the examples shown in the preceding chart.
You have attempted of activities on this page.