Java

OOP GUIDE / WORK

 

WORDMATCH CLASS

 

Topics

  • Method implementation
  • Finding number of occurrences of a string in another string
  • Comparing two strings alphabetically (using compareTo)

 

Source

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

 

 

TASK – PART 1 – SETUP – WORDMATCH CLASS STARTER CODE

 

Copy and paste the following starter code to your IDE.  Other than the implementation of the constructor (one line of code), this is the exact code given in the question.

 

 

public class WordMatch

{

     /** The secret string. */

     private String secret;

    

    

     /** Constructs a WordMatch object with the given secret string of lowercase letters. */

 

     public WordMatch(String word)

     {

           secret = word;

     }

    

    

     /** Returns a score for guess, as described in part (a).

      * Precondition: 0 < guess.length() <= secret.length()

      */

    

     public int scoreGuess(String guess)

     {

           /* to be implemented in part (a) */

     }

    

    

     /** Returns the better of two guesses, as determined by scoreGuess and the rules for a

      * tie-breaker that are described in part (b).

      * Precondition: guess1 and guess2 contain all lowercase letters.

      * guess1 is not the same as guess2.

      */

    

     public String findBetterGuess(String guess1, String guess2)

     {

           /* to be implemented in part (b) */

     }

}

 

 

 

TASK – PART 2 – WORK

 

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

 

Do both Part A and Part B in the Question.

 

 

TASK – PART 3 – TESTING YOUR CLASS

 

Copy and paste the following code that will test your WordMatch class.  Note that the program below matches the examples from the questions so you should compare your results with the results in the examples.  You should also consider adding your own tests.

 

 

public class WordMatchTester

{

     public static void main(String[] args)

     {

           //PART A EXAMPLE #1

           WordMatch game = new WordMatch("mississippi");

           System.out.println(game.scoreGuess("i"));  //4

           System.out.println(game.scoreGuess("iss"));  //18

           System.out.println(game.scoreGuess("issipp"));  //36

           System.out.println(game.scoreGuess("mississippi"));  //121

          

           System.out.println("---");

          

           //PART A EXAMPLE #2

           WordMatch game2 = new WordMatch("aaaabb");

           System.out.println(game2.scoreGuess("a"));   //4

           System.out.println(game2.scoreGuess("aa"));  //12

           System.out.println(game2.scoreGuess("aaa"));  //18

           System.out.println(game2.scoreGuess("aabb"));  //16

           System.out.println(game2.scoreGuess("c"));  //0

 

           System.out.println("---");

          

           //PART B EXAMPLE

           WordMatch game3 = new WordMatch("concatenation");

           System.out.println(game3.scoreGuess("ten"));  //9

           System.out.println(game3.scoreGuess("nation"));  //36

           System.out.println(game3.findBetterGuess("ten","nation"));  //nation

           System.out.println(game3.scoreGuess("con"));  //9

           System.out.println(game3.scoreGuess("cat"));  //9

           System.out.println(game3.findBetterGuess("con", "cat"));  //con

     }

}