AP Computer Science A FRQ Walkthrough

FRQ Topic: SignedText Class

Overview

This AP Computer Science A FRQ asks you to write the complete SignedText class. The class works with a person's name and adds a signature to a piece of text.

Big Idea: This FRQ tests your understanding of instance variables, constructors, String methods, conditionals, and return values.

What the Class Needs

The SignedText class needs the following parts:

Step 1: Understand the Signature Rules

The method getSignature() creates a signature using the first name and last name.

Rule 1: Empty First Name

If the first name is empty, the signature is only the last name.

new SignedText("", "Wong")

The signature would be:

Wong

Rule 2: Non-Empty First Name

If the first name is not empty, the signature uses the first letter of the first name, followed by a dash, followed by the last name.

new SignedText("henri", "dubois")

The signature would be:

h-dubois
Example:
new SignedText("GRACE", "LOPEZ")
returns
G-LOPEZ

Step 2: Decide the Instance Variables

Since the constructor receives a first name and a last name, the class needs to store both values.

private String firstName;
private String lastName;

These variables are called instance variables because they belong to each individual SignedText object.

Step 3: Write the Constructor

The constructor receives the first and last name as parameters and saves them in the instance variables.

public SignedText(String first, String last)
{
    firstName = first;
    lastName = last;
}
Reminder: A constructor has the same name as the class and does not have a return type.

Step 4: Write getSignature()

The getSignature() method returns the signature as a String.

First, check whether the first name is empty.

firstName.equals("")

Another possible way to check this is:

firstName.length() == 0

Solution for getSignature()

public String getSignature()
{
    if (firstName.equals(""))
    {
        return lastName;
    }

    return firstName.substring(0, 1) + "-" + lastName;
}

Why Use substring(0, 1)?

In Java, substring(0, 1) gets the first character of a String as a String.

"henri".substring(0, 1)

This returns:

h

Step 5: Understand addSignature(String text)

The method addSignature(String text) receives a String and returns a possibly changed version of that String.

This method has three possible cases:

Case What Should Happen?
The signature does not appear Add the signature to the end of the text
The signature already appears at the end Return the original text unchanged
The signature appears at the beginning Move the signature from the beginning to the end
Important: The FRQ states that the signature appears at most once and only at the beginning or the end. This makes the logic easier.

Step 6: Use getSignature() Inside addSignature()

Instead of rebuilding the signature again, call the method getSignature().

String sig = getSignature();

This is good programming because it avoids repeating code.

Step 7: Check if the Signature Is at the End

If the text already ends with the signature, return the text unchanged.

if (text.endsWith(sig))
{
    return text;
}
Example:
Text: Best wishesFOX
Signature: FOX
Result: Best wishesFOX

Step 8: Check if the Signature Is at the Beginning

If the text starts with the signature, remove it from the beginning and add it to the end.

if (text.startsWith(sig))
{
    return text.substring(sig.length()) + sig;
}

How This Works

text.substring(sig.length()) removes the signature from the front of the text.

Example:
Text: FOXThanks
Signature: FOX

Remove FOX from the front:
Thanks

Add FOX to the end:
ThanksFOX
Another Example:
Text: G-LOPEZHello
Signature: G-LOPEZ
Result: HelloG-LOPEZ

Step 9: If the Signature Is Missing

If the signature is not at the beginning and not at the end, add it to the end.

return text + sig;
Example:
Text: Dear
Signature: FOX
Result: DearFOX

Complete Solution

public class SignedText
{
    private String firstName;
    private String lastName;

    public SignedText(String first, String last)
    {
        firstName = first;
        lastName = last;
    }

    public String getSignature()
    {
        if (firstName.equals(""))
        {
            return lastName;
        }

        return firstName.substring(0, 1) + "-" + lastName;
    }

    public String addSignature(String text)
    {
        String sig = getSignature();

        if (text.endsWith(sig))
        {
            return text;
        }

        if (text.startsWith(sig))
        {
            return text.substring(sig.length()) + sig;
        }

        return text + sig;
    }
}

Student-Friendly Algorithm

  1. Store the first name and last name in instance variables.
  2. In getSignature():
    • If the first name is empty, return only the last name.
    • Otherwise, return the first letter of the first name, a dash, and the last name.
  3. In addSignature(String text):
    • Save the signature in a variable.
    • If the text already ends with the signature, return the text unchanged.
    • If the text starts with the signature, remove it from the front and add it to the end.
    • Otherwise, add the signature to the end.

Key Java Concepts Used

Common Mistakes to Avoid

Quick Practice Questions

Question 1

What would this return?

SignedText s = new SignedText("Anna", "Smith");
s.getSignature();

Answer:

A-Smith

Question 2

What would this return?

SignedText s = new SignedText("", "Wong");
s.getSignature();

Answer:

Wong

Question 3

If the signature is FOX, what does this return?

addSignature("Hello")

Answer:

HelloFOX

Question 4

If the signature is FOX, what does this return?

addSignature("FOXHello")

Answer:

HelloFOX

Question 5

If the signature is FOX, what does this return?

addSignature("HelloFOX")

Answer:

HelloFOX

Final Takeaway

The most important part of this FRQ is carefully checking where the signature appears. If it is missing, add it. If it is already at the end, leave it alone. If it is at the beginning, move it to the end.