15-121 Fall 2020 [Reid-Mller]

LAB 2: Scrabble Scorer

EXERCISES

In the game of Scrabble, players create words on a crossword puzzle grid using letter tiles. Each letter tile has a value:

Players can also use a blank tile (worth 0 points) to substitute for any letter. The score for a word is the sum of the values of the letter tiles.

  1. Write a simple Java program that in the main method prompts the user to enter a word and outputs the Scrabble score for that word, based on the point values of the letters only using the helper methods below. A blank tile should be entered as an asterisk (*). For this part, you may assume that the input word is valid and consists of only lowercase letters.

  2. In your solution, you should include a method getLetterScore that returns the Scrabble point value of a given character. If the character is not a letter, return 0. Use the following method header. (Discuss with your partner, why the method is private and static.)

    private static int getLetterScore(char letter)

    Try to write your method so you don't test for each letter individually, using an if or switch statement. HINT: Set up an array to hold all of the letter scores, and then use the position of each letter in the alphabet to extract out the corresponding score from the array. If you store the letters of the alphabet in a string, you can use the indexOf method on this string to get the position of any letter of the alphabet.

    Since the letter scores and alphabet do not change, you can make these class constants so that it is created just once. That is, after the class header declare and assign an initializer list to your array using the syntax illustrated in the example below.

    private static final int[] DATA = {1, 2, 8, 7}; // use all capitals for constants
    
    Similarly, you can make the alphabet string a class constant.
  3. Next write a static method getWordScore that returns the score for the word provided as a String parameter. Should it be private or public? Call this method from your main method.
  4. Test your code to see that it works correctly before moving on.

    Sample tests:

    WORD          WORD SCORE
    java          14
    program       12
    equals        15
    cla*s         6
    banana        8
    mississippi   17
    zzyzx         42
    j*llybea*     19
    

  5. The game also has bonus points. If a tile is placed on one of the following four spaces, the player earns a bonus:

    Also, if the word is 7 or more letters, an additional 50 points is added to the score after all other bonuses are computed.

    Modify getWordScore so that it updates the total score for bonuses. You may assume that there is at most one double letter score and one triple letter score. You may also assume there is at most one double or triple word score (but not both). Include a parameter for the method that is Scanner object so that you can continue to get input from the user. Prompt the user as follows:

    Is there a DOUBLE LETTER SCORE? [y/n]  y
      Which letter?  e
    Is there a TRIPLE LETTER SCORE? [y/n]  n
    Is there a DOUBLE WORD SCORE? [y/n]  y
    
    Note that since there is a double word score, the program does not prompt for a triple word score. You may assume for this part that the user input is always valid.

    Derive a set of test cases to use that will execute all possible paths through your code that computes bonuses. In a comment in your program, write in the number of different execution paths there are in this part of the program. Test your program thoroughly with the test cases you derived.

HANDIN

At the end of Lab, create a .zip file of your Lab2 project and upload it to autolab. If you worked with another student, you only have to submit one program. Be sure that both of you keep a copy for yourselves.

FUTURE WORK: FUN STUFF FOR LATER

Modify your program so that all user input is checked for validity. If any user input is invalid, the user should be prompted to input again until the input is valid. Things to check: