Free-Response Question 1: Methods and Control Structures. Students will write two methods or one constructor and one method of a given class based on the provided specifications and examples. In Part A (4 points), the method or constructor will require students to write iterative or conditional statements, or both, as well as statements that call methods in the specified class.
There are very few examples of FRQ1B, since it has only existed since 2025-2026. You will see several examples of Strings being used in FRQs 2-3 in the following lessons.
Subsection15.6.2Practice FRQ - The EmailFilter Class
A system is being designed to "sanitize" email subject lines by removing specific "flagged" words and replacing them with a placeholder. You will write the method sanitizeSubject.
The sanitizeSubject method takes two parameters: a String subject and a String flaggedWord. The method should return a new String where every occurrence of flaggedWord in subject is replaced by the string "[REDACTED]".
/**
* Returns a version of the subject where every instance of flaggedWord
* is replaced by "[REDACTED]".
* Precondition: subject and flaggedWord are not null.
*/
public String sanitizeSubject(String subject, String flaggedWord) {
// TO BE IMPLEMENTED IN PART (B)
}
The Infinite Loop: If you donβt update the string you are searching (or donβt move your index), the indexOf will keep finding the same word forever.
The "Last Bit" Bug: Many students forget to add result += remaining at the very end. Without it, your code would cut off the end of the sentence (e.g., "Check out this [REDACTED]" instead of "Check out this [REDACTED] offer!").
A system is being designed to "sanitize" email subject lines by removing specific "flagged" words and replacing them with a placeholder. You will write the method sanitizeSubject.
The sanitizeSubject method takes two parameters: a String subject and a String flaggedWord. The method should return a new String where every occurrence of flaggedWord in subject is replaced by the string "[REDACTED]".
Subsection15.6.3Practice FRQ - The SequenceAnalyzer Class
In bioinformatics, scientists analyze DNA strings composed of the characters A, C, G, and T. A "doublet" is defined as any two-character sequence where a character is immediately followed by the same character (e.g., "AA", "GG").
Write the method countValidDoublets. This method analyzes a given DNA string and counts how many doublets exist, but only if the doublet appears after the first occurrence of a "start marker" character.
public class SequenceAnalyzer {
/**
* Counts doublets (two identical consecutive characters) that appear AFTER the first occurrence
* of startMarker. Precondition: dna is not null, dna.length() >= 2.
*/
public static int countValidDoublets(String dna, String startMarker) {
/* TO BE IMPLEMENTED */
}
}
Activity15.6.2.
In bioinformatics, scientists analyze DNA strings composed of the characters A, C, G, and T. A "doublet" is defined as any two-character sequence where a character is immediately followed by the same character (e.g., "AA", "GG").
Write the method countValidDoublets. This method analyzes a given DNA string and counts how many doublets exist, but only if the doublet appears after the first occurrence of a "start marker" character.
A software company is creating a "Short-Hand Expander" that converts common abbreviations into their full-word equivalents. You will write a method for the TextExpander class that finds and replaces specific abbreviations.
Write the method expandText, which takes three String parameters: message, abbr, and fullWord. The method should return a new String where every occurrence of the abbreviation abbr in message is replaced by fullWord.
/**
* Returns a version of message where every instance of abbr
* is replaced by fullWord.
* Precondition: message, abbr, and fullWord are not null.
*/
public String expandText(String message, String abbr, String fullWord) {
/* TO BE IMPLEMENTED BELOW */
}
A software company is creating a "Short-Hand Expander" that converts common abbreviations into their full-word equivalents. You will write a method for the TextExpander class that finds and replaces specific abbreviations.
Write the method expandText, which takes three String parameters: message, abbr, and fullWord. The method should return a new String where every occurrence of the abbreviation abbr in message is replaced by fullWord.