AP Computer Science A 2025 – FRQ Question 2 SignedText Solution & Scoring Guide
AP Computer Science A 2025 – FRQ Question 2 (SignedText) Solution & Scoring Guide
Overview
Question 2 asks you to write the complete SignedText class, including:
- private instance variables
- a constructor
-
getSignaturemethod -
addSignaturemethod
The class works with signatures built from a first and last name. The signature is either:
- just the last name, if the first name is empty, or
- the first letter of the first name, followed by
"-", followed by the last name.
The addSignature method uses this signature and either appends it to the text, leaves the text unchanged, or moves the signature from the beginning to the end, depending on where the signature already appears.
Canonical SignedText Solution (9 Points)
public class SignedText
{
private String firstName;
private String lastName;
public SignedText(String first, String last)
{
firstName = first;
lastName = last;
}
public String getSignature()
{
String sig = "";
if (!firstName.equals(""))
{
sig += firstName.substring(0, 1) + "-";
}
sig += lastName;
return sig;
}
public String addSignature(String textStr)
{
String sig = getSignature();
int index = textStr.indexOf(sig);
if (index == -1)
{
// case 1: signature not present
return textStr + sig;
}
else if (index == 0)
{
// case 2: signature at beginning
return textStr.substring(sig.length()) + sig;
}
else
{
// case 3: signature at end (or somewhere else treated as "leave as is")
return textStr;
}
}
}
How the Solution Works
1. Class, Fields, and Constructor (Scoring Points 1–3)
-
Class header:
public class SignedText– Correctly declares the class (no parentheses, no invalid modifiers). -
Instance variables:
private String firstName;private String lastName;
– Both areprivateand store the two pieces of data we need. -
Constructor:
public SignedText(String first, String last)– Takes twoStringparameters and initializes the instance variables:firstName = first;lastName = last;
These pieces together earn the points for declaring the class, declaring and initializing the instance variables correctly, and writing the proper constructor header.
2. getSignature Method (Scoring Points 4–7)
Goal: Return the correct signature string based on the rules.
- If the first name is an empty string, return just the last name.
- Otherwise, return first-letter-of-first-name +
"-"+ last name.
Implementation details:
public String getSignature()
{
String sig = "";
if (!firstName.equals(""))
{
sig += firstName.substring(0, 1) + "-";
}
sig += lastName;
return sig;
}
-
Empty vs non-empty first name: The check
!firstName.equals("")distinguishes the two cases. -
String methods: Uses
substring(0, 1)to get the first letter of the first name, and string concatenation to build the signature. -
Correct result in both cases:
- First name empty →
sigstarts as"", skip theif, then addlastName. - First name non-empty → prefix
first letter + "-", then addlastName.
- First name empty →
This earns the method-header point, the comparison-with-empty-string point, the “correct signature in both cases” point, and demonstrates correct use of String methods.
3. addSignature Method (Scoring Points 8–9)
Goal: Return a new string based on where (if at all) the signature appears in the parameter.
We first compute the object’s signature and find its position in the given text:
public String addSignature(String textStr)
{
String sig = getSignature();
int index = textStr.indexOf(sig);
if (index == -1)
{
// case 1: signature not present
return textStr + sig;
}
else if (index == 0)
{
// case 2: signature at beginning
return textStr.substring(sig.length()) + sig;
}
else
{
// case 3: signature at end (or anywhere else → unchanged)
return textStr;
}
}
The three required cases:
-
Case 1 – No signature in the text (
index == -1):
We simply append the signature:return textStr + sig; -
Case 2 – Signature at the beginning (
index == 0):
We remove the signature from the front and append it to the end:textStr.substring(sig.length())is the original text without the leading signature.
So we return:textStr.substring(sig.length()) + sig; -
Case 3 – Signature at the end / elsewhere (
index > 0):
The signature already ends the text, so we leave the string unchanged:return textStr;
By clearly separating these three situations and returning the correct string in each case, this method earns the remaining points for identifying the cases and implementing the correct string-manipulation algorithm.
Alternate Valid Approach (Single Signature Field)
An alternative (also full-credit) solution is to store the signature itself as a single instance variable and build it once in the constructor:
public class SignedText
{
private String signature;
public SignedText(String first, String last)
{
signature = last;
if (first.length() > 0)
{
signature = first.substring(0, 1) + "-" + last;
}
}
public String getSignature()
{
return signature;
}
public String addSignature(String textStr)
{
String result = textStr;
int index = textStr.indexOf(signature);
if (index < 0)
{
result += signature;
}
else if (index == 0)
{
result = result.substring(signature.length()) + signature;
}
return result;
}
}
This version still follows all the same rules, but instead of storing first and last name separately, it stores only the final signature string.
Key Takeaways for FRQ 2 (SignedText)
- Write a correct class header and keep instance variables
private. - Initialize all instance variables properly in the constructor using its parameters.
- Use
Stringmethods likelength,substring, andindexOfcorrectly and with valid indices. - Handle all required cases in
addSignature: no match, match at start, match at end. - Always return a
Stringthat matches the behavior described in the prompt and examples.