SignedText is #2 from the from the 2025 AP Computer Science A Free Response problems.

https://apcentral.collegeboard.org/media/pdf/ap25-frq-computer-science-a.pdf

SignedText class

public class SignedText
{
    private String first, last;

    public SignedText(String first, String last)
    {
        this.first = first;
        this.last = last;
    }

    public String getSignature()
    {
        if(first.length() > 0)
            return first.substring(0, 1) + "-" + last;
        else
            return last;
    }

    public String addSignature(String message)
    {
        String signature = getSignature();
        int index = message.indexOf(signature);

        if(index == 0)
            return message.substring(signature.length()) + signature;
        else if(index == -1)
            return message + signature;
        else
            return message;
    }
}

Java files with test code

SignedText.java
TwoTest.java

See Running JUnit 5 tests.

2025 AP CS Exam Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

See an error? Question? Please comment below.

Comment on SignedText