LogMessage is #2 from the from the 2016 AP Computer Science A Free Response problems.

https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap16_frq_computer_science_a.pdf

Part (a) LogMessage constructor

public LogMessage(String message)
{
    int colonIndex = message.indexOf(":");
    machineId = message.substring(0, colonIndex);
    description = message.substring(colonIndex + 1);
}

The constructor requires that a String be split into 2 parts. See Strings on the AP CS A Exam for an explanation of String methods used on the AP CS A Exam.

Part (b) containsWord method

public boolean containsWord(String keyword)
{
    int keywordIndex = description.indexOf(keyword);

    while(keywordIndex != -1)
    {
        int beforeIndex = keywordIndex - 1;
        int afterIndex = keywordIndex + keyword.length();

        if((beforeIndex == -1 ||
                description.substring(beforeIndex, beforeIndex + 1).equals(" ")) &&
                (afterIndex == description.length() ||
                description.substring(afterIndex, afterIndex + 1).equals(" ")))
            return true;

        keywordIndex = description.indexOf(keyword, keywordIndex + 1);
    }

    return false;
}

The good folks at Skylit have a clever solution to part (b).

This problem is a simpler version of a method in the AP CS Magpie Lab that identifies words that are not contained within other words. The method in the Magpie Lab checked for any non-letter characters surrounding the desired word, while this method checks only for spaces.

This solution uses the 2 parameter indexOf method, as does the AP CS Magpie Lab. The alternate solution below sticks to the single parameter indexOf method from the Quick Reference.

Part (b) containsWord method (alternate solution)

public boolean containsWord(String keyword)
{
    for(int i = 0; i < description.length(); i++)
    {
        int keywordIndex = description.substring(i).indexOf(keyword);

        if(keywordIndex != -1)
        {
            int beforeIndex = keywordIndex - 1 + i;
            int afterIndex = keywordIndex + keyword.length() + i;

            if((beforeIndex == -1 ||
                    description.substring(beforeIndex, beforeIndex + 1).equals(" ")) && 
                    (afterIndex == description.length() ||
                    description.substring(afterIndex, afterIndex + 1).equals(" ")))
                return true;
        }
    }

    return false;
}

This solution uses only the indexOf method with a single parameter from the Quick Reference.

Part (c) removeMessages method

public List<LogMessage> removeMessages(String keyword)
{
    ArrayList<LogMessage> removedMessages = new ArrayList<LogMessage>();

    int i = 0;
    while(i < messageList.size())
    {
        if(messageList.get(i).containsWord(keyword))
            removedMessages.add(messageList.remove(i));
        else
            i++;
    }

    return removedMessages;
}

See ArrayList practice for details on adding to and removing from an ArrayList within a loop.

2016 AP CS Exam Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on LogMessage