Java

OOP GUIDE / WORK

 

WORDPAIRLIST CLASS

 

Topics

  • Advanced class design
  • Aggregation
  • ArrayLists

 

Source

  • This question was part of the 2018 AP exam free response questions.  You can find a link to all free response questions on the previous page.

 

 

TASK – PART 1 – SETUP – WORDPAIR CLASS

 

Copy and paste the following class to your IDE.  Note that a toString() method was added for convenience.

 

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;

     }

 

     public String toString()

     {

           return "(\"" + first + "\", \"" + second + "\")";

     }

}

 

 

TASK – PART 2 – SETUP – STARTER CODE FOR WORDPAIRLIST CLASS

 

Copy and paste the starting code for the WordPairList class.  A toString() method was added for convenience.

 

import java.util.ArrayList;

 

public class WordPairList

{

     /** The list of word pairs, initialized by the constructor. */

 

     private ArrayList<WordPair> 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) */ }

 

 

     public String toString()

     {

           return allPairs.toString();

     }

}

 

 

TASK – PART 3 – WORDPAIRLIST CLASS

 

Click here for a PDF containing the work that you must do. 

 

Do both Part A and Part B in the Question.

 

 

TASK – PART 4 – TESTING YOUR CLASS

 

Copy and paste the following code that will test your WordPairList class.  Note that the program below matches the examples from the questions so you should compare your results with the results in the examples.

 

 

public class WordListTester

{

     public static void main(String[] args)

     {

           //EXAMPLE 1 FROM THE QUESTION

           String[] wordNums = {"one", "two", "three"};

           WordPairList exampleOne = new WordPairList(wordNums);

           System.out.println(exampleOne);

          

           //EXAMPLE 2 FROM THE QUESTION

           String[] phrase = {"the", "more", "the", "merrier"};

           WordPairList exampleTwo = new WordPairList(phrase);

           System.out.println(exampleTwo);

          

           //AN EXTRA EXAMPLE

           String[] letters = {"AA", "BB", "CC", "DD", "EE", "FF", "GG"};

           WordPairList extraExample = new WordPairList(letters);

           System.out.println(extraExample);

          

           //EXAMPLE 3 FROM THE QUESTION (IN PART B)

           String[] moreWords = {"the", "red", "fox", "the", "red"};

           WordPairList exampleThree = new WordPairList(moreWords);

           System.out.println(exampleThree);

           System.out.println(exampleThree.numMatches());

     }

}