Free-Response Question 2: Class Design. Students will be instructed to design and implement a class based on provided specifications and examples. A second class might also be included. Students will be provided with a scenario and specifications in the form of a table demonstrating ways to interact with the class and the results. The class must include a class header, instance variables, a constructor, a method, and implementation of the constructor and required method.
This question involves the SignedText class, which contains methods that are used to include a signature as part of a string of text. You will write the complete SignedText class, which contains a constructor and two methods.
The SignedText constructor takes two String parameters. The first parameter is a first name and the second parameter is a last name. The length of the second parameter is always greater than or equal to 1.
The getSignature method takes no parameters and returns a formatted signature string constructed from the first and last names according to the following rules.
If the first name is not an empty string, the returned signature string is the first letter of the first name, a dash ("-"), and the last name, in that order.
The addSignature method returns a possibly revised copy of its String parameter. The parameter will contain at most one occurrence of the objectβs signature, at either the beginning or the end of the parameter. The returned string is created from the parameter according to the following rules.
If the objectβs signature does not occur in the String parameter of the method, the returned String is the value of the parameter with the signature added to the end.
If the objectβs signature occurs at the beginning of the String parameter, the returned String is the value of the original parameter with the signature removed from the beginning and appended to the end of the parameter.
The following table contains a sample code execution sequence and the corresponding results. The code execution sequence appears in a class other than SignedText.
This question involves methods that distribute text across lines of an electronic sign. The electronic sign and the text to be displayed on it are represented by the Sign class. You will write the complete Sign class, which contains a constructor and two methods.
The Sign class constructor has two parameters. The first parameter is a String that contains the message to be displayed on the sign. The second parameter is an int that contains the width of each line of the sign. The width is the positive maximum number of characters that can be displayed on a single line of the sign.
A sign contains as many lines as are necessary to display the entire message. The message is split among the lines of the sign without regard to spaces or punctuation. Only the last line of the sign may contain fewer characters than the width indicated by the constructor parameter.
The following are examples of a message displayed on signs of different widths. Assume that in each example, the sign is declared with the width specified in the first column of the table and with the message "Everything on sale, please come in", which contains 34 characters.
The following table contains a sample code execution sequence and the corresponding results. The code execution sequence appears in a class other than Sign.