Status: Started
  • default.txt
Font Size
14
Parentheses autocomplete
Wrap lines
Editor
Theme
Code with blocks by default
Console Font Size
12
Console Theme
Display Server Graphics Screen
Show File Tab Bar
Debug Mode
2 PhraseEditor Part a (text only)
default.txt
/** 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.
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Output
Docs
Exercise
More
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
This student is viewing this assignment in English. View this page in English?

5 points

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

{

private String currentPhrase;


/** Constructs a new Phrase object. */

public Phrase(String p)

{ currentPhrase = 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 findLastOccurrence(String str)

{ /* to be implemented in part (b) */ }


/** Returns a string containing the current phrase. */

public String toString()

{ return currentPhrase; }

}


Part 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, currentPhrase remains unchanged.


Several examples of the behavior of the method replaceNthOccurrence are shown below.


Class information for this question

public class Phrase

private String currentPhrase

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()


The Phrase class includes the method findNthOccurrence, which returns the nth occurrence of a given string. You must use findNthOccurrence appropriately to receive full credit.


Complete method replaceNthOccurrence.

Reset Code

Slides and Notes

No slides available for this video

About

Java (main)
Java Version 1.8.0_222