Computer Science A
2025 FRQ 2
- FRQ Question 2
- Part A (Entire Question): Build the
SignedTextclass - Scoring Guidelines (What your solution must demonstrate)

FRQ Question 2
This question involves the SignedText class, which contains methods that are used to include a signature as part of a string of text.
You will write the complete SignedText class, which contains:
- a constructor
- two methods:
getSignatureandaddSignature
Analyze the Problem: Walkthrough
Signature Formatting + String Manipulation
Problem Type: String processing / conditionals
Difficulty: Medium
You are given a
SignedTextobject created from:
firstName(String)lastName(String, length ≥ 1)The object must be able to:
- Build a signature in the required format (
getSignature)- Ensure a piece of text ends with that signature (
addSignature)
Mental Model
Think of the signature as a fixed “stamp” this object generates.
getSignature()produces the stamp.addSignature(text)makes sure the stamp ends up at the end of the text:- If it’s missing → add it
- If it’s already at the end → leave it alone
- If it’s at the beginning → move it to the end
Checkpoint: Before coding, make sure you can answer:
- What EXACT string does
getSignature()return whenfirstNameis""?- How do you check whether a string starts with / ends with something?
- What operations let you remove the beginning portion and re-attach it to the end?
Part A (Entire Question): Build the SignedText class
The SignedText constructor takes two String parameters:
- the first parameter is a first name
- the second parameter is a last name (length ≥ 1)
You must implement:
1) getSignature()
Returns a formatted signature string constructed from the names:
- If
firstNameis an empty string, return just the last name. - Otherwise, return:
- the first letter of the first name
- a dash
"-" - the last name
in that order
Examples:
("", "Wong")→"Wong"("henri", "dubois")→"h-dubois"("GRACE", "LOPEZ")→"G-LOPEZ"(keep original capitalization)
2) addSignature(String str)
Returns a possibly revised copy of str.
The parameter will contain at most one occurrence of the object’s signature, and it will be either:
- at the beginning, OR
- at the end, OR
- not present
Rules:
- If the signature does not occur in
str→ returnstr + signature - If the signature occurs at the end → return
strunchanged - If the signature occurs at the beginning → remove it from the beginning and append it to the end
Walkthrough (How to Think About It)
Step 1: Store the names
- Save
firstNameandlastNameinto instance variables so both methods can use them.
Step 2: Build the signature
- Handle the special case
firstName.equals("") - Otherwise, use
firstName.substring(0, 1)to get the first character.
Step 3: Handle the 3 addSignature cases
- Compute
sig = getSignature()once - Use:
str.startsWith(sig)str.endsWith(sig)
- For the “signature at the beginning” case:
- remove the first
sig.length()characters - then append
sig
- remove the first
Common pitfall: don’t accidentally remove characters from the middle.
The problem guarantees the signature can only be at the beginning or the end (or not present).
Example Table (from the prompt)


Code Runner Challenge
Implement the SignedText class, including the constructor
View IPYNB Source
// CODE_RUNNER: Implement the SignedText class, including the constructor
// and the methods getSignature and addSignature
public class SignedText
{
private String firstName;
private String lastName;
/**
* Constructs a SignedText object with the provided first and last name.
* Precondition: last has length >= 1
*/
public SignedText(String first, String last)
{
firstName = first;
lastName = last;
}
/**
* Returns a formatted signature string constructed from the
* first and last names, as described in the prompt.
*/
public String getSignature()
{
if (firstName.equals(""))
{
return lastName;
}
return firstName.substring(0, 1) + "-" + lastName;
}
/**
* Returns a possibly revised copy of str based on the rules
* for adding or repositioning the object's signature.
*/
public String addSignature(String str)
{
String sig = getSignature();
if (str.endsWith(sig))
{
return str;
}
else if (str.startsWith(sig))
{
return str.substring(sig.length()) + sig;
}
else
{
return str + sig;
}
}
/* There may be instance variables, constructors, and methods that are not shown. */
}
// Runner: Do Not Edit
public class Main {
public static void main(String[] args) {
SignedText st1 = new SignedText("", "Wong");
System.out.println(st1.getSignature()); // Wong
SignedText st2 = new SignedText("henri", "dubois");
System.out.println(st2.getSignature()); // h-dubois
SignedText st3 = new SignedText("GRACE", "LOPEZ");
System.out.println(st3.getSignature()); // G-LOPEZ
SignedText st4 = new SignedText("", "FOX");
String text = "Dear";
System.out.println(st4.addSignature(text)); // DearFOX
text = "Best wishesFOX";
System.out.println(st4.addSignature(text)); // Best wishesFOX
text = "FOXThanks";
System.out.println(st4.addSignature(text)); // ThanksFOX
text = "G-LOPEZHello";
System.out.println(st3.addSignature(text)); // HelloG-LOPEZ
}
}
Main.main(null);
Scoring Guidelines (What your solution must demonstrate)
Even though this FRQ is not split into separate “Part A / Part B” sections, graders are looking for all required behaviors across the constructor and both methods.
Constructor
A correct solution:
- Stores the provided names in instance variables
- Uses the exact values passed in (do not force uppercase/lowercase)
getSignature()
A correct solution:
- Checks whether
firstNameis the empty string - If empty → returns only
lastName - Otherwise → returns
first letter of firstName + "-" + lastName - Preserves the original capitalization of names
addSignature(String str)
A correct solution:
- Computes the object’s signature (typically via
getSignature()) - Handles the 3 required cases:
1) Signature not present
Return str + signature
2) Signature at end
Return str unchanged
3) Signature at beginning
Return str with the beginning signature removed, then appended to the end
Important: The prompt guarantees there is at most one signature occurrence, and it is only at the beginning or end. Your code should rely on that to stay clean and simple.
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)
{
return textStr + sig;
}
else if (index == 0)
{
return textStr.substring(sig.length()) + sig;
}
else
{
return textStr;
}
}
}
Guidelines:


Why this approach works
getSignature()is deterministic: it always builds the same signature from the stored names.addSignature()checks the simplest “already correct” condition first (endsWith).- Only if needed, it handles the “signature at the beginning” case using
substring. - If neither beginning nor end matches, the signature must be missing, so we append it.
// Optional: quick additional edge tests (not required by the prompt, but useful for confidence)
SignedText a = new SignedText("A", "Z");
System.out.println(a.getSignature()); // A-Z
System.out.println(a.addSignature("Hi")); // HiA-Z
System.out.println(a.addSignature("HiA-Z")); // HiA-Z
System.out.println(a.addSignature("A-ZHi")); // HiA-Z