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 by Chris Thiel, OFMCap */ int i= 0; int cnt = 0; while ( cnt < n-1 && currentPhrase.indexOf(str, i) >= 0 ) { i = currentPhrase.indexOf(str, i) + 1; cnt++; } return currentPhrase.indexOf(str, i); } /** 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) */ return -2019; } /** Returns a string containing the current phrase. */ public String toString() { return currentPhrase; } public static void main(String[] args) { System.out.println("2017: Phrase Tester\n"); Phrase p1 = new Phrase( "A cat ate late."); System.out.println("Part (a)\n For \""+p1+"\" replaceNthOccurrence\n"); System.out.println("(\"at\", 1, \"rane\") should return \"A crane ate late.\""); p1.replaceNthOccurrence("at", 1, "rane"); System.out.println("Your code: \"" + p1 +"\"\n"); p1 = new Phrase( "A cat ate late."); Phrase p4 = new Phrase("aaaa"); System.out.println("(\"at\", 6, \"xx\") should return \"A cat ate late.\""); p1 = new Phrase( "A cat ate late."); p1.replaceNthOccurrence("at", 6, "xx"); System.out.println("Your code: \"" + p1 +"\"\n"); System.out.println("(\"bat\", 2, \"xx\") should return \"A cat ate late.\""); p1.replaceNthOccurrence("bat", 2, "xx"); System.out.println("Your code: \"" + p1 +"\"\n"); System.out.println("Part (a)\n For \""+p4+"\" replaceNthOccurrence\n"); System.out.println("(\"aa\", 1, \"xx\") should return \"xxaa\""); p4.replaceNthOccurrence("aa", 1, "xx"); System.out.println("Your code: \"" + p4 +"\"\n"); System.out.println("(\"aa\", 2, \"bbb\") should return \"abbba\""); p4 = new Phrase("aaaa"); p4.replaceNthOccurrence("aa", 2, "bbb"); System.out.println("Your code: \"" + p4 +"\"\n"); p1 = new Phrase( "A cat ate late."); System.out.println("Part (b)\n For \""+p1+"\" findLastOccurrence\n"); System.out.println("findLastOccurrence(\"at\") should return 11 "); System.out.println("Your code returns: \"" + p1.findLastOccurrence("at") +"\"\n"); System.out.println("findLastOccurrence(\"cat\") should return 2 "); System.out.println("Your code returns: \"" + p1.findLastOccurrence("cat") +"\"\n"); System.out.println("findLastOccurrence(\"bat\") should return -1 "); System.out.println("Your code returns: \"" + p1.findLastOccurrence("bat") +"\"\n"); } }