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.
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 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.
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.
Subsection15.9.2FRQ 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.
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.