Programming Project: Max Scrabble Score The board game Scrabble works by assigni
ID: 3577052 • Letter: P
Question
Programming Project: Max Scrabble Score
The board game Scrabble works by assigning points to wooden tiles arranged in cells on a board. It's described here: Scrabble on Wikipedia.
For this project, you will write code that computes the Scrabble score for each line of text in a file and reports the line with the maximal Scrabble score and that score.
Computing the Scrabble Score
The Scrabble score for a line of text is the sum of the scores for all of the individual characters in that line. The Scrabble score for a single character is computed as follows:
For each character in the line:
If the character is a letter, get the letter score. If it is not a letter, its score is zero. The case of the letter (upper/lower case) does not matter.
If the position of the character (position is its index- the first character is position 0) is divisible by 4, the score is doubled.
If the position of the character is divisible by 9, the score is tripled.
If the position of the character is divisible by 4 and 9, the score is doubled.
Required Code Structure
This project utilizes three source files: A driver class called ScrabbleDriver, a class that opens the file and uses a Scanner to access the lines of text called TextFileAccessor, and the MaxScrabbleScore class- which you will write for this project. The MaxScrabbleScore class must extend the TextFileAccessor class. The starter files can be downloaded here:
MaxScrabbleStarterFiles.zip
Testing Your Code
Further Requirements
Your MaxScrabbleScore class must properly extend the TextFileAccessor class. There must not be any unnecessary duplication of superclass code in MaxScrabbleScore.
Your MaxScrabbleScore class must compile and run with the other two source files provided with this project. You may not modify these files.
Your MaxScrabbleScore class must produce the output in the same format as shown in the above section Testing Your Code. Points off if your values are correct but you don't match the output as shown above.
Your code for the MaxScrabbleScore class must use an int array to store the letter scores given in the table above. This array must be used to compute the individual letter scores in the lines of text.
Letter ABC DE FG H I J KLM N O P Q R S T U V W X Y Z Score 1 31 3 21 1 4 24 18 51 3 1 1 3 10 1 1 1 1 4 4 5 4 10Explanation / Answer
import java.util.Scanner;
import java.io.IOException;
import java.io.FileReader;
//Class Scribble Driver defined
public class ScrabbleDriver
{
//Main method
public static void main(String args[])
{
//Exception handling
try
{
//Scanner class object created for accepting file name
Scanner s = new Scanner(System.in);
//Accepts the file name
System.out.println("Enter the file name: ");
String fileName = s.nextLine();
TextFileAccessor scrab = new MaxScrabbleScore();
//Calls the openFile method of TextFileAccessor class
scrab.openFile(fileName);
//Calls the process file method of TextFileAccessor class
scrab.processFile();
//Calls the report string format method of TextFileAccessor class
System.out.println(scrab.getReportStr());
}
catch(IOException ioex)
{
System.out.println(ioex);
}
}
}
//Abstract class TextFileAccessor
abstract class TextFileAccessor
{
protected String fileName;
protected Scanner scan;
//Throws a file not found exception if can't open file
public void openFile(String fn) throws IOException
{
fileName = fn;
scan = new Scanner (new FileReader(fileName));
}
public void processFile()
{
//Checks for the data availability
while(scan.hasNext())
{
//Calls the processLine method by sending a line of text to it
processLine(scan.nextLine());
}
//Close the scanner class
scan.close();
}
//Abstract methods
protected abstract void processLine(String line);
public abstract String getReportStr();
}
//Class Max Scribble Score class derived from TextFileAccessor class
class MaxScrabbleScore extends TextFileAccessor
{
//Creates an array for Scrabble letter scores
char letter[] = {'A','B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
int point[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
int score = 0;
String l;
//Abstract method process Line defined
protected void processLine(String line)
{
l = line;
//Loops till end of the string received
for(int c = 0; c < line.length(); c++)
{
//Checks whether it is a character or not
if(Character.isLetter(line.charAt(c)))
{
//Loops till end of the scrabble letters
for(int d = 0; d < letter.length; d++)
{
//Converts character of the file and checks it with the scrabble letters
if(Character.toUpperCase(line.charAt(c)) == letter[d])
{
//If the index position is divisible by 4 double the score
if(c % 4 == 0)
score += point[d] * 2;
//If the index position is divisible by 9 triple the score
else if(c % 9 == 0)
score += point[d] * 3;
//If the index position is divisible by 4 and 9 double the score
else if(c % 4 == 0 && c % 9 == 0 )
score += point[d] * 2;
//If not add the score
else
score += point[d];
}
}
}
}
}
//Abstract method get Report Str defined
public String getReportStr()
{
String s = ("Max scrabble score: " + String.valueOf(score) + " : for this line: " + l);
return s;
}
}
Outupt 1:
Enter the file name:
test.txt
Max scrabble score: 29 : for this line: now is the time
Output 2:
Enter the file name:
Sampletext.txt
Max scrabble score: 40 : for this line: and aid. their. party?
Output 3:
Enter the file name:
HeartOfDarkness.txt
Max scrabble score: 192 : for this line: plenty time. i can manage. you take kurtz away quick--quick--i tell
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.