15-121 SPRING 2010 [CORTINA]

LAB 0: Review of Java Basics

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 prompts the user to enter a word and outputs the Scrabble score for that word, based on the point values of the letters only. 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.

    In your solution, you should include a method that returns the Scrabble point value of a given character. If the character is not a letter, return 0. 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 as we did in lecture, you can use the indexOf method on this string to get the position of any letter of the alphabet.

    To initialize an array directly, you can use the syntax illustrated in the example below:

    int[] data = {1, 3, 2, 7, 4};
    

    To access the array, simply use the array name and a subscript in square brackets. For example, the following code adds up the integers in the data array declared above:

    int sum = 0;
    for (int i = 0; i < data.length; i++)
       sum += data[i];
    

    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
    

  2. 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 your program 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). 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

If you worked with another student, put both of your names in a comment at the beginning of your program. At the end of lab, create a zip file of your program and submit it to the handin server http://handin.intro.cs.cmu.edu/v1. (If you worked together, you only have to submit one program.)

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: