Other 2018 FR Questions FR other years Be Prepared Home
A-2
Part (a)
  public WordPairList(String[] words)
  {
    allPairs = new ArrayList<WordPair>();

    for (int i = 0; i < words.length - 1; i++)
      for (int j = i+1; j < words.length; j++)
        allPairs.add(new WordPair(words[i], words[j]));
  }
Part (b)
  public int numMatches()
  {
    int count = 0;

    for (WordPair pair : allPairs)
      if (pair.getFirst().equals(pair.getSecond())) 1
        count++;

    return count;
  }
Notes:
  1. The WordPair class in the question does not show or mention any fields. Fields are required to hold the first and the second words of the pair, and these fields must be private. For example,
      private String first, second;
    Or:
      private String[] words;
    Regardless of how the fields are defined in WordPair, you cannot refer to them directly in your solution in WordPairList code because the fields are private in WordPair. You must call the getFirst and getLast accessor methods.

Other 2018 FR Questions | Back to Contents

Copyright © 2018 by Skylight Publishing
support@skylit.com