Question: question part A part B 3. This question involves analyzing and modifying a string. The following phrase class maintains a phrase in an instance variable

question
question part A part B 3. This question involves analyzing and modifying
part A
a string. The following phrase class maintains a phrase in an instance
variable and has methods that access and make changes to the phrase.
part B
You will write two methods of the Phrase class. public class Phrase
public private String current Phrase; /** Constructs a new Phrase object. */

3. This question involves analyzing and modifying a string. The following phrase class maintains a phrase in an instance variable and has methods that access and make changes to the phrase. You will write two methods of the Phrase class. public class Phrase public private String current Phrase; /** Constructs a new Phrase object. */ public Phrase (String p) I current Phrase = p; } + /** Returns the index of the nth occurrence of str in the current phrase: returns -1 if the nth occurrence does not exist. Precondition: str.length() > 0 and n > 0 * Postcondition: the current phrase is not modified. public int findnthoccurrence (String str, int n) { /* implementation not shown */ } /** Modifies the current phrase by replacing the nth occurrence of str with repl. * If the nth occurrence does not exist, the current phrase is unchanged. * Precondition: str.length() > 0 and n > 0 public void replaceNthoccurrence (String str, int n, String repl) { /* to be implemented in part (a) */ } /** Returns the index of the last occurrence of str in the current phrase: returns -1 if str is not found. * Precondition: str.length() > 0 * Postcondition: the current phrase is not modified. public int findLast Occurrence (String str) { /* to be implemented in part (b) */ } /** Returns a string containing the current phrase. */ public String toString() { return current Phrase; } ) (a) Write the phrase method replaceNthoccurrence, which will replace the nth occurrence of the string str with the string repl. If the nth occurrence does not exist, current Phrase remains unchanged. Several examples of the behavior of the method replacenthoccurrence are shown below. Output produced Code segments Phrase phrasel - new Phrase ("A cat ate late."); phrasel.replaceNthOccurrence ("at", 1, "rane"); System.out.println(phrasel); A crane ate late. Phrase phrase2 - new Phrase ("A cat ate late."); phrase2.replaceNthOccurrence ("at", 6, "XX"); System.out.println (phrase); A cat ate late. Phrase phrase3 = new Phrase ("A cat ate late."); phrase3.replaceNthoccurrence ("bat", 2, "XX"); System.out.println (phrase); A cat ate late. Phrase phrase4 = new Phrase "aaaa"); phrase4 replaceNthoccurrence ("aa", 1, "xx"); System.out.println (phrasel); xxaa Phrase phrase5 = new Phrase ("aaaa"): phrases.replacenthoccurrence ("aa", 2, "bbb"); System.out.println (phrase5); abbba Class information for this question public class Phrase private String current Phrase public Phrase (String p) public int findnthoccurrence (String str, int n) public void replaceNthoccurrence (String str, int n, String repl) public int findLast Occurrence (String str) public String toString() The Phrase class includes the method findthoccurrence, which returns the nth occurrence of a given string. You must use findthoccurrence appropriately to receive full credit. Complete method replaceNthoccurrence below. /** Modifies the current phrase by replacing the nth occurrence of str with repl. * If the nth occurrence does not exist, the current phrase is unchanged. * Precondition: str.length() > 0 and n > 0 */ public void replaceNthoccurrence (String str, int n, String repl) (b) Write the Phrase method findlastoccurrence. This method finds and returns the index of the last occurrence of a given string in current Phrase. If the given string is not found, -1 is returned. The following tables show several examples of the behavior of the method findLast Occurrence. Phrase phrasel = new Phrase ("A cat ate late."); Value returned Method call phrasel.findLastOccurrence ("at") 11 phrasel.findlastoccurrence ("cat") N phrasel.findLast Occurrence ("bat") -1 Class information for this question public class Phrase private String current Phrase public Phrase(String p) public int findnthoccurrence (String str, int n) public void replacenthoccurrence (String str, int n, string repl) public int findLastOccurrence (String str) public String toString() WRITE YOUR SOLUTION ON THE NEXT PAGE. You must use findNthoccurrence appropriately to receive full credit. Complete method findLastOccurrence below. * /** Returns the index of the last occurrence of str in the current phrase; returns - 1 if str is not found. Precondition: str.length() > 0 Postcondition: the current phrase is not modified. * public int findLastOccurrence (String str)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!