2. This question involves the StatusLabel class, which represents a label used to tag file names with a status. You will write the complete StatusLabel class, which contains a constructor and two methods.
StatusLabel objects are created by calls to a constructor with two parameters.
The first parameter is a String representing the status text (e.g., "Draft"). (Precondition: Assume that this value is a non-empty string.)
The second parameter is a boolean that indicates whether the status is urgent.
getFormattedTag
The StatusLabel class contains a getFormattedTag method, which returns a string representing the formatted status label based on the object's state.
The getFormattedTag method takes no parameters.
If the urgent parameter passed to the constructor was false, the returned string consists of the status text enclosed in square brackets (e.g., "[Draft]").
If the urgent parameter passed to the constructor was true, the returned string consists of the status text enclosed in square brackets with three asterisks on each side of the text (e.g., "[Draft]").
Returns the formatted status string.
processFileName
The StatusLabel class contains a processFileName method, which applies the status label to a given file name.
The method takes a single String parameter representing a file name.
The method first obtains the formatted tag by calling the getFormattedTag method.
If the file name parameter already contains the formatted tag, the method returns the file name unchanged.
If the file name ends with the extension ".txt", the method returns a new string with the formatted tag inserted immediately before the ".txt" extension.
In all other cases, the method returns a new string with the formatted tag appended to the end of the file name.
Returns the modified file name string containing the status label.
Statement | Return Value | Explanation |
|---|---|---|
StatusLabel s1 = new StatusLabel("Draft", false); | s1 is created with status text "Draft" and is not urgent. | |
String t1 = s1.getFormattedTag(); | "[Draft]" | Since s1 is not urgent, the tag is enclosed in simple brackets. |
String f1 = s1.processFileName("essay.doc"); | "essay.doc[Draft]" | The file name does not end in .txt and does not contain the tag, so the tag is appended to the end. |
String f2 = s1.processFileName("notes.txt"); | "notes[Draft].txt" | The file name ends in .txt, so the tag is inserted before the extension. |
StatusLabel s2 = new StatusLabel("Secret", true); | s2 is created with status text "Secret" and is urgent. | |
String t2 = s2.getFormattedTag(); | "[Secret]" | Since s2 is urgent, the tag is enclosed in brackets with asterisks. |
String f3 = s2.processFileName("plan[Secret].pdf"); | "plan[Secret].pdf" | The file name already contains the formatted tag, so the original string is returned. |
Write the complete StatusLabel class. Your implementation must meet all specifications and conform to the examples shown in the table.
00:00