The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 337 Weeks Ago, 3 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Java Programming Project: Max Scrabble ScoreOverview
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.
Learning Objectives
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:
This table lists the Scrabble letter scores:
A-1
B-3
C-3
D-2
E-1
F-4
G-2
H-4
I-1
J-8
K-5
L-1
M-3
N-1
O-1
P-3
Q-10
R-1
S-1
T-1
U-1
V-4
W-4
X-8
Y-4
Z-10
For example, given this line of text:
Dave, I am afraid.
The Scrabble score for that line would be: 38
Note that the first position of the input string is zero, which is divisible by 4 and 9, so the character score for the first letter, D, which is 2, is doubled.
Another example: this line:
now is the time
has a Scrabble score of 29.
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.
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);}
}
}
------------------------------------------------------------------------------------------
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();
}
------------------------------------------------------------------------------------------------
public class MaxScrabbleScore extends TextFileAccessor {
//declare variables
public MaxScrabbleScore(){
//initialise variables in constructor
}
protected void processLine(String curLine) {
// process each character from the current line
//calculate sum of the scores for characters in that line
}
private int getScrabbleScore(char c, int pos){
//helper method to return the score for the character
}
public String getReportStr(){
//returns the score and the string that was entered
}
}//end class
Testing Your Code
Strongly Suggested:
PROJECT REQUIREMENTS
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly