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:
- If the text starts with the signature, return the text with the signature removed from the beginning
- Otherwise, if the text ends with the signature, return the text with the signature removed from the end
- 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.