## nwalser

The board game scrabble works by assigning points to wooden tiles arranged in cells on a board. It's described here:http://en.wikipedia.org/wiki/Scrabble. We'll simplify this considerably, and consider the following question. We begin with the letter-scoring scheme from Scrabble: a = 1, b = 3, c = 3, d = 2, ..., z = 10. Given a text file - a novel for example, and a word size, say 7 characters, what is the highest-(or a highest-) scoring word in the the file of that word size?

Your solution should include two files: A driver, which we provide below, and which you must use, and a file called Scrabble.java, which does the heavy lifting for the application. The Scrabble file should extend Echo, in the standard way we've indicated (it can also extend the LineReader class from Chapter 11 of the text).

Tips:

Make sure you read Chapter 10 of the text as a prelude to doing this problem.

Here is a list from a to z of the letter scores from Scrabble:

{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}

It's far easier to convert lines of text to lowercase before processing.

Use a StringTokenizer object to chop up a line into words.

Handle characters that show up inside words, such as apostrophe and hyphen this way: treat a word like "can't" as a five character word, and score the apostrophe as 0. Also, use this String as the second parameter to your StringTokenizer constructor: ",.?! {}[];:". The result of using this String will mean that a word like "students'" (note apostrophe at end) should have length 9.

This is the driver they have given us to use:

``````import java.util.*;
import java.io.*;

public class ScrabbleDriver{
public static void main(String args[])
{
try{
Scanner s = new Scanner(System.in);
System.out.println("enter file name, then word size");
String f = s.next();
int size = s.nextInt();
Scrabble scrab = new Scrabble(f,size);
scrab.reportWinner();
}
catch(Exception e)
{System.out.println(e);}
}
}``````

I am not sure how to go about coding this so if anyone could help that would be great.

## BestJewSinceJC 700

The Scrabble file should extend Echo, in the standard way we've indicated (it can also extend the LineReader class from Chapter 11 of the text).

Java classes can only extend one other class, there is no multiple inheritance (some might argue about interfaces but meh). Anyway, what I would do is the following:

1. Write a Word class that has two variables - the String for the word itself and an integer for the word's point value.
2. In your Word class, implement the Comparable interface and write your implementation of the compareTo method so that it sorts according to point value.
3. Create an ArrayList of Word Objects, ArrayList<Word>
5. Create a Word Object for each word in the file, add Word to the ArrayList
6. Sort the ArrayList using Collections.sort(yourList);
7. Get the user's input, see what it is asking, then use your sorted list to answer the question. For example, if it asks what the highest point value is for a 7 letter word, then start at the end of the list (since it is already sorted by point value) and find the first 7 letter word. That is your answer.

Hope that helps. If you're only allowed to write one file called Scrabble.java then implement the Word class as an inner class. Chances are your teacher wants you to manually sort the list, and have two lists (one for the word and one for its point value). This is much harder, and worse OOP, than the implementation I just explained to you above.

## nwalser

I forgot to put that we were also given this code to work with to create our class:

``````import java.util.Scanner;
import java.io.*;

public class Echo{
String fileName; // external file name
Scanner scan; // Scanner object for reading from external file

public Echo(String f) throws IOException
{
fileName = f;
scan = new Scanner(new File(fileName));
}
while(scan.hasNext()){
processLine(scan.nextLine());
}
scan.close();
}
public void processLine(String line){ // does the real processing work
System.out.println(line);
}
}``````

I don't know if this would change anything about what you said above but just figured id let you know to be sure.

## BestJewSinceJC 700

It doesn't change anything I said except that in step 4 you should use your teacher's class to read stuff.

## nwalser

alright so im trying to figure out how to actually begin writing this but I am still having trouble. Can you assist?

## BestJewSinceJC 700

Start by doing step 1. Then step 2. Continue until you're done step 7. Let us know what you've tried and what you don't understand.

## nwalser

alright 1. was simple but im not sure what to do for 2. This is what im using right now:

``````public class Scrabble extends Echo{

public Word(String word, int val){
wordUsed = word;
pointValue = val;

}``````

## BestJewSinceJC 700

To be honest, I thought you were well above the skill level that you are at. The project your teacher has given you implies that you should be familiar with classes and methods; both using other class methods and implementing your own methods. That said, the suggestions I gave you previously are probably way too complex. I'll continue to explain them if you're still interested, but I won't be offended if you don't want to continue, so let me know.

You can't put a constructor called Word in a class called Scrabble. You must either implement a separate class called Word, or you must use an inner class called Word. See inner classes. I put together a little example to demonstrate what I mean. I left implementing the Comparable interface up to you; notice that it says the class implements Comparable, but I left the compareTo method blank (you need to provide the code for it). This link explains pretty thoroughly how to implement the Comparable interface.

``````import java.util.ArrayList;

public class OuterClass {

ArrayList<Word> list = new ArrayList<Word>();

public static void main(String[] args){
OuterClass c = new OuterClass();
Word wrd = c.new Word("Something", 10);
System.out.println(c.list.get(0).toString());
}

public class Word implements Comparable{
String theWord;
int pointValue;

public Word(String word, int val){
theWord = word;
pointValue = val;
}

public String toString(){
return theWord;
}

@Override
public int compareTo(Object arg0) {
//You need to implement this method!
return 0;
}
}

}``````

You should also note that in order to do your project you don't *have* to implement Comparable. Implementing Comparable means that your Objects are capable of being sorted. Since you don't have to sort your Objects in order to find the word with the highest score, you don't have to implement Comparable. If you don't sort them, what you could do instead is to make a list of all of your words, then search through the list, looking at every word, and seeing if the word is the length you want and has the highest number of points.

Another thing you should be aware of is that it is also possible to do this project without any Word Objects. You could, for example, store your (String) words in one array and not even bother to keep track of their point values. Of course, this would mean that every time your teacher input something like "what is the highest value word with 7 letters" you would have to calculate the point value for every word with 7 letters, which is horribly inefficient compared to doing it once and storing the result.

## jwenting 1,649

To be honest, I thought you were well above the skill level that you are at.

and no doubt so assumes the teacher because he's been pushing knowledge for quite a while.
Can't blame the man if kids aren't paying attention, rather trying to hack school networks to get to their facebook accounts.

## nwalser

alright you were confusing me with what you were saying before so I wrote this whole new code but it still has a couple errors and was wondering if you could help me solve them.

``````public class Scrabble extends Echo{
private int wordLength;
private int[] score = new int[26];
private int maxScore = 0;
private String maxScWord = " ";

public Scrabble(String fn, int wl) throws IOException{
super(fn);
wordLength = wl;
for(int i = 0; i < 26; i++)
score[i] = i % 10;
}

public void processLine(String line){
line = line.toLowerCase();
StringTokenizer st = new StringTokenizer(line, " /':!;,. ");
while(st.hasMoreTokens()){
processToken(st.nextToken());
}
}

public void processToken(String token){
if(wordLength = token.length()){
int tokenScore = 0;
for(int i = 0; i < token.length(); i++){
Char c = token.charAt(i);
if((c == '\') \\ (c == '-'))
tokenScore = tokenScore;

else{
int ind = (int)(c - 'a');
if((ind >= 0) && (ind <= 25))
tokenScore = tokenScore + score[ind];
}
}
}
}

if(tokenScore > maxScore){
maxScore = tokenScore
maxScWord = token
}

public void report(){
System.out.print("MaxScore: " + maxScore + "Word: " + maxScWord);
}``````

## BestJewSinceJC 700

You should explain your design step by step (like I did in my post above). Then tell us what pieces of code/ methods you have implemented for each part of the solution. Then we will tell you what you are doing wrong. That is how to code. Design first, then start coding.