2025 AP CSA FRQ 2: SignedText

2025 FRQ #2: SignedText

Class Design and String Manipulation

9 Points Total
Jump to: Write Class Common Mistakes Related FRQs
Practice Timer (25 min recommended)
25:00

Overview

This question involves designing a class to manage text signatures, including creating signatures and adding/removing them from text.

TopicsClass Design, Constructors, Strings
Difficulty Medium
Time~25 minutes
TypeClass Design

Problem Description

This question involves a class that manages text with signatures. A SignedText object stores a signature string and provides methods to work with that signature.

Signature Format

A signature is created from a person's first name and last name. The signature consists of the entire first name followed by the first character of the last name.

For example, if the first name is "Ada" and the last name is "Lovelace", the signature would be "AdaL".

Class Requirements

You will write the complete SignedText class with:

  • Constructor: Takes a first name and last name, creates the signature
  • getSignature(): Returns the signature string
  • addSignature(String text): Adds or removes signature from text based on rules below

addSignature Method Rules

The addSignature method takes a text string and returns a modified version according to these rules:

  1. If the text starts with the signature, return the text with the signature removed from the beginning
  2. Otherwise, if the text ends with the signature, return the text with the signature removed from the end
  3. Otherwise, return the text with the signature appended to the end

Example Execution

Code Return Value Explanation
SignedText st = new SignedText("Ada", "Lovelace"); Creates SignedText with signature "AdaL"
st.getSignature() "AdaL" Returns the signature
st.addSignature("Hello World") "Hello WorldAdaL" No signature present, so append it
st.addSignature("AdaLHello") "Hello" Starts with signature, so remove from start
st.addSignature("GoodbyeAdaL") "Goodbye" Ends with signature, so remove from end

Precondition: The last name parameter will always have length ≥ 1.

Write the Complete Class

Write the complete SignedText class, including:

  • Any necessary instance variables
  • A constructor that takes two String parameters: first name and last name
  • The getSignature method
  • The addSignature method

Your implementation must meet all specifications described above and match the example behavior shown in the table.

Class Signature: public class SignedText

Write Your Solution

Check Your Code

🔓 View Solution & Rubric

Scoring Rubric

Points Criterion
+1 Declares class correctly
+1 Private instance variable for signature
+1 Constructor creates signature (firstName + first char of lastName)
+1 getSignature returns the signature
+1 addSignature checks if text starts with signature
+1 addSignature removes from start correctly using substring
+1 addSignature checks if text ends with signature
+1 addSignature removes from end correctly using substring
+1 addSignature appends signature when not present

Solution

public class SignedText
{
    private String signature;
    
    public SignedText(String firstName, String lastName)
    {
        signature = firstName + lastName.substring(0, 1);
    }
    
    public String getSignature()
    {
        return signature;
    }
    
    public String addSignature(String text)
    {
        if (text.startsWith(signature))
        {
            return text.substring(signature.length());
        }
        else if (text.endsWith(signature))
        {
            return text.substring(0, text.length() - signature.length());
        }
        else
        {
            return text + signature;
        }
    }
}

Explanation

The class stores the signature as an instance variable. The constructor builds it from firstName + first character of lastName. The addSignature method uses startsWith and endsWith to check position, then substring to remove or concatenation to append.

⚠️ Common Mistakes to Avoid

Wrong substring indices

For removing from end: substring(0, text.length() - signature.length())

Checking end before start

Must check startsWith FIRST per the specification

Forgetting the else case

If signature not at start or end, append it

Related Practice: Class Design

Similar FRQs:

Study Guide:

Daily Practice:

Contact form