Java program How would I write this program? In most word games, each letter in
ID: 3674702 • Letter: J
Question
Java program
How would I write this program?
In most word games, each letter in a word is scored according to its point value, which is inversely proportional to its frequency in English words. In Scrabble, the points and letter distribution are allocated as shown in the table. Write a Java program that reads in a word (not a sentence) and prints out the score in Scrabble, not counting any other bonuses that occur in the game. Make sure the program handles blank tiles. Represent a blank tile with a #. For example, the Scrabble word "FARM"is worth 9 points: 4 for the F, 1 each for the A and the R, and 3 for the M. However, the word SYZYGY-should be worth: 1 + 4 +10+ 4 + 2 + 0 = 21 points since Y is only found twice a blank tile must be used. Check for the proper number of tiles- assume a maximum of seven letters on a rack as well as ignore any characters other than uppercase letters in computing the score. Output should look similar to below. Sample Run: Enter a word: SYZYGY English-language editions of Scrabble contain 100 letter tiles, in the following distribution: 2 blank tiles 1 point: E Times 12, A Times 9, 1 Times 9, O Times 8, N Times 6, R Times 6, T Times 6, L Times 4, S Times 4, U Times 4 2 points: D Times 4, G Times 3 3 points: B Times 2, C Times 2, M Times 2, P Times 2 4 points: F Times 2, H Times 2, V Times 2, W Times 2, Y Times 2 5 points: K Times 1 8 points: J Times 1, X Times 1 10 points: Q Times 1, Z Times 1 Word: SYZYG# Scrabble score is: 1 + 4 + 10 + 4 + 2 + 0 = 21 Name the program: ModifiedScrabbleScoreXX.java, where XX are your initials.Explanation / Answer
import acm.program.*;
public class ScrabbleCalc extends ConsoleProgram
{
public void run()
{
String word = readLine("Enter Scrabble word (all in uppercase) and we'll calculate your score: ");
print(scrabbleScore(word));
}
private int scrabbleScore(String scrabbleWord) {
int score = 0;
for (int i = 0; i < scrabbleWord.length(); i++){
char calculatedLetter = scrabbleWord.charAt(i);
switch (calculatedLetter) {
case 'A':
case 'E':
case 'I':
case 'L':
case 'N':
case 'O':
case 'R':
case 'S':
case 'T':
case 'U':
score +=1; break;
case 'D':
case 'G':
score +=2; break;
case 'B':
case 'C':
case 'M':
case 'P':
score +=3; break;
case 'F':
case 'H':
case 'V':
case 'W':
case 'Y':
score +=4; break;
case 'K':
score +=5; break;
case 'J':
case 'X':
score +=8; break;
case 'Q':
case 'Z':
score +=10; break;
default: break;
}
}
return score;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.