Phrase

The third question on the 2017 AP Computer Science exam asked you to process a string, replacing the \(Nth\) occurrence and finding the last occurrence.

As part of the question you were given a non-implemented method called findNthOcurrence that you were expected to use. It looked like this on the test.

public int findNthOccurrence( String str, int n ) {
    /* Implementation not shown */
}

If they're going to give you a method stub like this, you should expect to use it somewhere in your solution. They wouldn't bother defining a method like this if you weren't expected to use it.

Part A

For part A you were to replace the \(Nth\) occurrence of a substring in currentPhrase. They gave you a series of examples like this.

Phrase phrase1 = new Phrase("A cat ate late.");
phrase1.replaceNthOccurrence("at", 1, "rane");
System.out.println(phrase1); 

This snippet should output "A crane ate late." assuming you implement replaceNthOccurrence correctly.

Here is a working solution.

public void replaceNthOccurrence( String str, int n, String repl ) {
    int idxStart = findNthOccurrence( str, n );
    if ( idxStart >= 0 ) {
        String front = currentPhrase.substring( 0, idxStart );
        String back = currentPhrase.substring( idxStart + str.length() );
        currentPhrase = front + repl + back;
    }
}

We find the index of the \(Nth\) occurrence using the provided findNthOccurrence method. Note that we don't see how findNthOccurrence is implemented, and we don't really have to care.

If idxStart is \(\ge\) 0 then the substring was found and we've got some work to do. If idxStart is \(\lt\) 0 then it wasn't found and we're not going to change currentPhrase.

I made a variable called front that contains everything up to the substring we're looking for and a variable back of everything after it. The code then returns front concatenated with repl followed by back which creates a string with the \(Nth\) occurrence of str replaced with repl.

Part B

The second part tasked you with finding the index of the last occurrence of a substring. And it specifically tells you that you must use findNthOccurrence to get full credit.

public int findLastOccurrence( String str ) {
    int lastIdx = -1;

    int n = 1;
    while ( findNthOccurrence( str, n ) > -1 ) {
        lastIdx = findNthOccurrence( str, n );
        n++; 
    }

    return lastIdx;
}

This code keeps incrementing n as it looks for the \(Nth\) occurrence. When that index value is -1, it means that the previous index was the last index.

This question bothered me a bit though. It seemed very arbitrary to have to solve this problem in this way when there's a one line solution that falls within the AP Java subset.

return str.lastIndexOf( str ); 

I'd imagine there will be a lot of packets with a similar solution written. Functionally using lastIndexOf would work, but wouldn't meet the requirement that you use the findNthOccurrence method.

Why ClassCube?

Are you spending too much time grading your students' source code?

ClassCube is an online tool for Computer Science teachers that allows you to create lab assignments for your students. They'll submit their code online and ClassCube will automatically grade it for you.

Teaching with ClassCube…

One thought on “Phrase

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>