if statements

Scrabble Score
Scrabble Score in Java
class Scrabble {
    private final int score;
    
    Scrabble(String word) {
        word = word.toUpperCase();
        int score = 0;
        
        for (int i = 0; i < word.length(); i++) {
            var ltr = word.substring(i, i + 1);
            if ("AEIOULNRST".contains(ltr))
                score += 1;
            else if ("DG".contains(ltr))
                score += 2;
            else if ("BCMP".contains(ltr))
                score += 3;
            else if ("FHVWY".contains(ltr))
                score += 4;
            else if ("K".contains(ltr))
                score += 5;
            else if ("JX".contains(ltr))
                score += 8;
            else if ("QZ".contains(ltr))
                score += 10;
        }
        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 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 series of if statements that checks each letter of the uppercased word. The letter is selected as a String by the substring() method and is passed to the contains() method for the String representing the letters for a particular score. The first if statement checks for the most common letters, and each succeeding if statement checks for letters less common than the statement before it. When the loop is done, the class-level score variable is set to the value of the local score variable.

29th May 2024 · Found it useful?