Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Project: Max Scrabble Score The board game Scrabble works by assigni

ID: 3578747 • 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

ScrabbleDriver.java:

import java.util.Scanner;
import java.io.IOException;

public class ScrabbleDriver{
public static void main(String args[])
{
try{
Scanner s = new Scanner(System.in);
System.out.println("Enter the file name:");
String fileName = s.nextLine();
TextFileAccessor scrab = new MaxScrabbleScore();
scrab.openFile(fileName);
scrab.processFile();
System.out.println(scrab.getReportStr());
}
catch(IOException ioex)
{System.out.println(ioex);}
}
}

TextFileAccessor.java:

import java.util.Scanner;
import java.io.IOException;
import java.io.FileReader;


public abstract class TextFileAccessor {
   protected String fileName;
   protected Scanner scan;

// throws a FileNotFoundException if can't open file
public void openFile(String fn)throws IOException {
    fileName = fn;
       scan = new Scanner(new FileReader(fileName));
}

   public void processFile() {
       while (scan.hasNext()) {
           processLine(scan.nextLine());
       }
       scan.close();
   }

   protected abstract void processLine(String line);

public abstract String getReportStr();

}

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.

Your code for the MaxScrabbleScore class may not import any Java libraries.

Thank you for the help!

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 10

Explanation / Answer

Here is the implementation of the MaxScrabbleScore class as indicated in the question. The ScrabbleDriver and TextFileAccessor class are not modified and same as in question. Please take the other 2 classes from your question.

public class MaxScrabbleScore extends TextFileAccessor {

   private static int scores[] = {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};  

   private int max_score;

   private String max_score_line;

   @Override

   protected void processLine(String line) {

       char word[] = line.toCharArray();

       char ch;

       int total = 0;

       int score ;

       for(int idx = 0; idx < word.length; idx++)

       {

           ch = word[idx];

           if(Character.isLetter(ch))

           {

               ch = Character.toUpperCase(ch); //convert to upper case

               score = scores[ch - 'A']; //using the ascii value of the character ch, get the index as ch - 'A'

           }

           else

               score = 0;

           if(idx % 9 == 0 && idx % 4 == 0) //divisible by both 4 and 9, double the score

               score *= 2;

           else if(idx % 9 == 0) //divisible by 9, triple the score

               score *= 3;

           else if(idx % 4 == 0) //divisible by 4 , double the score

               score *= 2;

          

           total += score;

              

       }

      

       if(total > max_score) //save the results is this line has maximum score

       {

           max_score = total;

           max_score_line = line;

       }

   }

   @Override

   public String getReportStr() {

       return "Max scrabble score: " + max_score +" for this line: " + max_score_line;

   }

}

input file: scrabble.txt

and aid. their. party?

plenty time. i can manage. you take kurtz away quick--quick--i tell

output

Enter the file name:

scrabble.txt

Max scrabble score: 192 for this line: plenty time. i can manage. you take kurtz away quick--quick--i tell