FRQ 2 - Class Design - Part 3

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period2 exercises9 graded tests

Files

README.mdDownload

FRQ 2 - Class Design - Part 3

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

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.

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.

The hints

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.

Example guesses and hints

Example guesses and hints

Exercise 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.

Open the 01-HiddenWordFRQ folder and edit Main.java. Press Run to try it; Run Tests checks it (3 tests). Name the class, then rename the file to match.

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.

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.

Frq2 combinedtable table

The last line of the chart illustrates that when the characteristics of a SingleTable change, so do those of the CombinedTable that contains it.

Exercise 2

Write the complete CombinedTable class. Your implementation must meet all specifications and conform to the examples shown in the preceding chart.

Open the 02-frq2-2021-combined-table folder and edit SingleTable.java. Press Run to try it; Run Tests checks it (6 tests). Name the class, then rename the file to match.

For teachers

FRQ 2 - Class Design - Part 3

Students read CSAwesome2's “FRQ 2 - Class Design - Part 3” in CS Room and work through its 2 Java coding exercises.

  • Time: 1 class period
  • Tests: 9 in 2 graded exercises, set up for you when you import the lesson

Goals

  • Practice for the AP exam's free-response questions.

Before class

In class

  1. Students read README.md.
  2. Students do exercises 01–02, each in its own folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-HiddenWordFRQMain.javaAutograded3
02-frq2-2021-combined-tableSingleTable.javaAutograded6

Teachers can download a reference solution after signing in.