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.
What the Class Needs
The SignedText class needs the following parts:
- Instance variables to store the first and last name
- A constructor
- A
getSignature()method - An
addSignature(String text)method
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
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;
}
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 |
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;
}
Text:
Best wishesFOXSignature:
FOXResult:
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.
Text:
FOXThanksSignature:
FOXRemove
FOX from the front:ThanksAdd
FOX to the end:ThanksFOX
Text:
G-LOPEZHelloSignature:
G-LOPEZResult:
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;
Text:
DearSignature:
FOXResult:
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
- Store the first name and last name in instance variables.
-
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.
-
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
Stringvariables- Instance variables
- Constructors
substring()length()equals()startsWith()endsWith()ifstatementsreturnstatements
Common Mistakes to Avoid
- Forgetting to create instance variables.
- Using
==instead of.equals()to compare Strings. - Forgetting that
substring(0, 1)gets only the first character. - Adding the signature twice when it is already at the end.
- Forgetting to remove the signature from the front before moving it to the end.
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