Java

OOP GUIDE / WORK

 

PHRASE CLASS

 

Topics

  • Class design
  • Finding the number of occurrences of a String in another String
  • String Manipulation

 

Source

  • This question was part of the 2017 AP exam free response questions.  You can find a link to all free response questions on the previous page.

 

 

TASK – PART 1 – SETUP – PHRASE CLASS STARTER CODE

 

Copy and paste the following starter code to your IDE.  Note that the method findNthOccurence(…) has been implemented in order to allow you to test your solutions.

 

 

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)

     {

           int index = currentPhrase.indexOf(str);    

           int occ = 1;

           while (index != -1 && occ < n)

           {

                index = currentPhrase.indexOf(str, index+1);

                occ++;

           }

           return index;        

     }

    

    

     /** 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;

     }

}

 

 

TASK – PART 2 – WORK

 

Click here for a PDF containing the work that you must do. 

 

Do both Part A and Part B in the Question.

 

 

TASK – PART 3 – TESTING YOUR CLASS

 

Copy and paste the following code that will test your Digits class.  Note that the program below matches the examples from the questions so you should compare your results with the results in the examples.  You should also consider adding your own tests.

 

 

public class PhraseTester

{

     public static void main(String[] args)

     {

           //EXAMPLES FROM PART A    

          

           Phrase phrase1 = new Phrase("A cat ate late.");

           phrase1.replaceNthOccurrence("at", 1, "rane");

           System.out.println(phrase1);           //A crane ate late.

          

           Phrase phrase2 = new Phrase("A cat ate late.");

           phrase2.replaceNthOccurrence("at", 6, "xx");

           System.out.println(phrase2);           //A cat ate late.

          

           Phrase phrase3 = new Phrase("A cat ate late.");

           phrase3.replaceNthOccurrence("bat", 2, "xx");

           System.out.println(phrase3);            //A cat ate late.

          

           Phrase phrase4 = new Phrase("aaaa");

           phrase4.replaceNthOccurrence("aa", 1, "xx");

           System.out.println(phrase4);            //xxaa

          

           Phrase phrase5 = new Phrase("aaaa");

           phrase5.replaceNthOccurrence("aa", 2, "bbb");

           System.out.println(phrase5);            //abbba

          

           //EXAMPLES FROM PART B

          

           Phrase phrase6 = new Phrase("A cat ate late.");

           System.out.println(phrase6.findLastOccurrence("at"));   //11

           System.out.println(phrase6.findLastOccurrence("cat"));  //2

           System.out.println(phrase6.findLastOccurrence("bat"));  //-1

     }

}