switch statement

Scrabble Score
Scrabble Score in Java
class Scrabble {
    private final int score;
    
    Scrabble(String word) {
        word = word.toLowerCase();
        int score = 0;
        
        for (int i = 0; i < word.length(); i++) {
            switch (word.charAt(i)) {
            case 'a', 'e', 'i', 'o', 'u', 'l', 'n', 'r', 's', 't':
                score += 1;
                break;
            case 'd', 'g':
                score += 2;
                break;
            case 'b', 'c', 'm', 'p':
                score += 3;
                break;
            case 'f', 'h', 'v', 'w', 'y':
                score += 4;
                break;
            case 'k':
                score += 5;
                break;
            case 'j', 'x':
                score += 8;
                break;
            case 'q', 'z':
                score += 10;
                break;
            default:
                break;
            }
        }
        this.score = score;
    }
    
    int getScore() {
        return score;
    }
}

This approach defines a private final variable to be returned by the getScore() method. It is private because it does not need to be directly accessed from outside the class, and it is final because its value does not need to be changed once it is set.

In the constructor, a local variable is defined for being updated in the for loop.

Note

Using the same name for a variable in a nested local scope that is used in its enclosing higher scope is called variable shadowing.

The variable is updated by a switch statement that checks each letter of the lowercased word.

Note

If most of the input will already be lower case, it is a bit more performant to normalize the input as lowercased, since fewer characters will need to be changed. However, it may be considered that to use upper case letters is more readable.

The letter is selected as a char by the charAt() method and is passed to the switch, with each case representing the letters for a particular score. When the loop is done, the class-level score variable is set to the value of the local score variable.

27th Nov 2024 · Found it useful?