Sprint View

2018 FRQ Question 2

5 min read

Question Overview

This question involves creating word pairs from an array of words. You will implement the WordPairList class that works with the provided WordPair class.

WordPair Class (Provided)

public class WordPair {
    private String first;
    private String second;
    
    public WordPair(String first, String second) {
        this.first = first;
        this.second = second;
    }
    
    public String getFirst() { return first; }
    public String getSecond() { return second; }
}

WordPairList Specification

The WordPairList class:

  • Instance Variable: An ArrayList<WordPair> called allPairs that contains all generated word pairs
  • Constructor: Takes a String[] array and creates pairs of each word with all words that come after it
  • numMatches(): Returns the count of word pairs where both words are the same

Key Insight

For an array of n words, you need to create pairs (word[i], word[j]) for all combinations where i < j. The order matters: “hi” paired with “there” is stored as (“hi”, “there”), not (“there”, “hi”).

Example 1: Basic Word Pairs

This shows how word pairs are generated from an array with no duplicate words.

Input: {"hi", "there", "bob", "smith"}

First Word Second Word WordPair Created
“hi” (index 0) “there” (index 1) (“hi”, “there”)
“hi” (index 0) “bob” (index 2) (“hi”, “bob”)
“hi” (index 0) “smith” (index 3) (“hi”, “smith”)
“there” (index 1) “bob” (index 2) (“there”, “bob”)
“there” (index 1) “smith” (index 3) (“there”, “smith”)
“bob” (index 2) “smith” (index 3) (“bob”, “smith”)

What you should see when you run the code:

All pairs: 6 total Number of matches: 0

Example 2: Word Pairs with Duplicates

This shows how matching pairs are counted when the array contains duplicate words.

Input: {"one", "two", "one", "three", "one"}

First Word Second Word Is Match?
“one” (index 0) “one” (index 2) Yes
“one” (index 0) “one” (index 4) Yes
“one” (index 2) “one” (index 4) Yes

What you should see when you run the code:

All pairs: 10 total Number of matches: 3

Code Runner Challenge

Implement WordPairList

View IPYNB Source
// CODE_RUNNER: Implement WordPairList
import java.util.ArrayList;

class WordPair {
    private String first;
    private String second;
    
    public WordPair(String first, String second) {
        this.first = first;
        this.second = second;
    }
    
    public String getFirst() { return first; }
    public String getSecond() { return second; }
    
    public String toString() { return first + " " + second; }
}

class WordPairList {
    private ArrayList<WordPair> allPairs;
    
    public WordPairList(String[] words) {
        allPairs = new ArrayList<WordPair>();
        
        for (int i = 0; i < words.length; i++) {
            for (int j = i + 1; j < words.length; j++) {
                allPairs.add(new WordPair(words[i], words[j]));
            }
        }
    }
    
    public int numMatches() {
        int count = 0;
        
        for (WordPair pair : allPairs) {
            if (pair.getFirst().equals(pair.getSecond())) {
                count++;
            }
        }
        
        return count;
    }
    
    public ArrayList<WordPair> getAllPairs() {
        return allPairs;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Testing Example 1:");
        String[] words1 = {"hi", "there", "bob", "smith"};
        WordPairList list1 = new WordPairList(words1);
        System.out.println("All pairs: " + list1.getAllPairs());
        System.out.println("Number of matches: " + list1.numMatches());
        
        System.out.println("Testing Example 2:");
        String[] words2 = {"one", "two", "one", "three", "one"};
        WordPairList list2 = new WordPairList(words2);
        System.out.println("All pairs: " + list2.getAllPairs());
        System.out.println("Number of matches: " + list2.numMatches());
    }
}
Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Solution Explanation

Here is the complete implementation of the WordPairList class that satisfies all 9 points on the rubric.

Part-by-part Breakdown

Instance Variable (1 point)

private ArrayList<WordPair> allPairs;
  • Stores all generated word pairs
  • Must be of type ArrayList<WordPair>

Constructor (4 points)

public WordPairList(String[] words) {
    allPairs = new ArrayList<WordPair>();
    
    for (int i = 0; i < words.length; i++) {
        for (int j = i + 1; j < words.length; j++) {
            allPairs.add(new WordPair(words[i], words[j]));
        }
    }
}
  • Initializes allPairs as an empty ArrayList
  • Uses nested loops: outer loop 0 to n-1, inner loop i+1 to n-1
  • Creates a new WordPair for each valid (i, j) combination where i < j
  • Adds each WordPair to the ArrayList

numMatches() (4 points)

public int numMatches() {
    int count = 0;
    
    for (WordPair pair : allPairs) {
        if (pair.getFirst().equals(pair.getSecond())) {
            count++;
        }
    }
    
    return count;
}
  • Initializes a counter variable to 0
  • Iterates through all WordPair objects in allPairs
  • Uses .equals() to compare strings (not ==!)
  • Returns the final count

How It Works

  1. When you create new WordPairList({"hi", "there", "bob"}), it generates all pairs where the first element comes before the second.
  2. The nested loops ensure we get: (“hi”,”there”), (“hi”,”bob”), (“there”,”bob”).
  3. numMatches() checks each pair and counts how many have identical first and second strings.
  4. This is useful for analyzing word patterns in text.

Scoring Guidelines

Points Breakdown (9 points total)

Part (a): Constructor (5 points)

  • 1 point: Initializes allPairs to a new ArrayList<WordPair>
  • 1 point: Outer loop iterates over indices of words from 0 to words.length - 2
  • 1 point: Inner loop iterates from the index after the outer loop’s current index to the end
  • 1 point: Creates new WordPair objects with words[i] and words[j] (in correct order)
  • 1 point: Adds each WordPair to allPairs

Part (b): numMatches() (4 points)

  • 1 point: Declares and initializes a counter variable to 0
  • 1 point: Iterates through allPairs (using for-each or indexed loop)
  • 1 point: Compares getFirst() and getSecond() using .equals() method
  • 1 point: Increments counter when strings match and returns the final count

Common Mistakes to Avoid

  • Using == instead of .equals() to compare strings
  • Starting the inner loop at the same index as the outer loop (should be i + 1)
  • Forgetting to initialize allPairs before adding elements
  • Swapping the order of words[i] and words[j] when creating WordPair objects
  • Using incorrect loop bounds (off-by-one errors)
  • Not returning the count value in numMatches()

Course Timeline