Answered step by step
Verified Expert Solution
Question
1 Approved Answer
APCS A-ArrayList (WordPair) This question involves reasoning about pairs of words that are represented by the following WordPair class. public class WordPair { /** Constructs
APCS A-ArrayList (WordPair) This question involves reasoning about pairs of words that are represented by the following WordPair class. public class WordPair { /** Constructs a WordPair object. */ public WordPair (String first, String second) { /* implementation not shown */ } /** Returns the first string of this WordPair object. */ public String getFirst() { /* implementation not shown */ } /** Returns the second string of this WordPair object. */ public String getSecond() { /* implementation not shown */ } } You will implement the constructor and another method for the following WordPairlist class. public class WordPairList { /** The list of word pairs, initialized by the constructor. */ private ArrayList allPairs; /** Constructs a WordPairList object as described in part (a). Precondition: words.length >= 2 public WordPairList (String[] words) { /* to be implemented in part (a) */ } /** Returns the number of matches as described in part (b). */ public int numMatches () { /* to be implemented in part (b) */ } } (a) Write the constructor for the WordPairList class. The constructor takes an array of strings words as a parameter and initializes the instance variable allPairs to an ArrayList of WordPair objects. A WordPair object consists of a word from the array paired with a word that appears later in the array. The allPairs list contains WordPair objects (words [i], words [j]) for every i and j, where 0 si= 2 */ public WordPairList (String[] words) (b) Write the WordPairlist method numMatches. This method returns the number of WordPair objects in allPairs for which the two strings match. For example, the following code segment creates a WordPairList object. String [] moreWords = {"the", "red", "fox", "the", "red" }; WordPairlist exampleThree = new WordPairList (morewords); After the code segment has executed, the allPairs instance variable of exampleThree will contain the following WordPair objects in some order. The pairs in which the first string matches the second string are shaded for illustration. ("the", "red"), ("the", "fox"), ("the", "the"), ("the", "red"), ("red", "fox"), ("red", "the"), ("red", "red"), ("fox", "the"), ("fox", "red"), ("the", "red") The call exampleThree.numMatches() should return 2. Complete method numMatches below. /** Returns the number of matches as described in part (b). public int numMatches ()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started