In: Computer Science
This is the code that needs to be completed...
import java.util.ArrayList;
import java.util.Collections;
/**
* Write a description of class SpellChecker here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SpellChecker
{
private ArrayList words;
private DictReader reader;
/**
* Constructor for objects of class SpellChecker
*/
public SpellChecker()
{
reader = new DictReader("words.txt");
words = reader.getDictionary();
}
/**
* This method returns the number of words in the dictionary.
* Change this.
*/
public int numberOfWords()
{
return 0;
}
/**
* This method returns true, if (and only if) the given word is
found in the dictionary.
* Complete this.
*/
public boolean isKnownWord(String word)
{
return false;
}
/**
* This method returns true if (and only if) all words in the given
wordList are found in the dictionary.
// Complete this.
*/
public boolean allKnown(ArrayList wordList)
{
return false;
}
/**
* This method tests the allKnown method. You do not need to change
this method.
*/
public boolean testAllKnown()
{
ArrayList testWords = new ArrayList();
testWords.add("Abu");
testWords.add("Chou");
if(allKnown(testWords))
{
return true;
}
else
{
return false;
}
}
/**
* This method returns a list of all words from the dictionary that
start with the given prefix.
* Complete this.
*/
public ArrayList wordsStartingWith(String prefix)
{
return null;
}
/**
* This method returns a list of all words from the dictionary that
include the given substring.
* Complete this.
*/
public ArrayList wordsContaining(String text)
{
return null;
}
/**
* Insert the given word into the dictionary.
* The word should only be inserted if it does not already exist in
the dictionary.
* If it does, the method does nothing.
* Make sure that the alphabetic order of the dictionary is
maintained.
* Complete this.
*/
public void insert(String newWord)
{
}
/**
* Remove the given word from the dictionary.
* If the word was successfully removed, return true.
* If not (for example it did not exist) return false.
*/ Complete this.
public boolean remove(String word)
{
return false;
}
/**
* Save the dictionary to disk.
* This is not meant to be hard – there is a method in the
DictReader class that you can use.
* Complete this
*/
public void save()
{
}
}
And this is the other class that is completed but helps run the incomplete code above.
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* Class DictReader offers functionality to load a dictionary from a
file,
* and to save it back to a file.
*
* To use this class, create an instance of this class with the file
name as
* a parameter. Then call 'getDictionary' to receive the dictionary
object.
*
* @author M. Kolling
* @version 2006-10-24
*/
public class DictReader
{
private ArrayList dict;
private String filename;
/**
* Create a DictReader instance from a file.
*/
public DictReader(String filename)
{
loadDictionary(filename);
this.filename = filename;
}
/**
* Return the dictionary as a list of words.
*/
public ArrayList getDictionary()
{
return dict;
}
/**
* Accept a new dictionary and save it under the same name. Use the
new
* one as our dictionary from now on.
*/
public void save(ArrayList dictionary)
{
try {
FileWriter out = new FileWriter(filename);
for(String word : dictionary) {
out.write(word);
out.write("\n");
}
out.close();
dict = dictionary;
}
catch(IOException exc) {
System.out.println("Error writing dictionary file: " + exc);
}
}
/**
* Load the dictionary from disk and store it in the 'dict'
field.
*/
private void loadDictionary(String filename)
{
dict = new ArrayList();
try {
BufferedReader in = new BufferedReader(new
FileReader(filename));
String word = in.readLine();
while(word != null) {
dict.add(word);
word = in.readLine();
}
in.close();
}
catch(IOException exc) {
System.out.println("Error reading dictionary file: " + exc);
}
}
}
The sourcecode for DictReader.java is given below:
package spellchecker;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* Class DictReader offers functionality to load a dictionary from a
file,
* and to save it back to a file.
*
* To use this class, create an instance of this class with the file
name as
* a parameter. Then call 'getDictionary' to receive the dictionary
object.
*
* @author M. Kolling
* @version 2006-10-24
*/
public class DictReader
{
private ArrayList dict;
private String filename;
/**
* Create a DictReader instance from a file.
*/
public DictReader(String filename)
{
loadDictionary(filename);
this.filename = filename;
}
/**
* Return the dictionary as a list of words.
*/
public ArrayList getDictionary()
{
return dict;
}
/**
* Accept a new dictionary and save it under the same
name. Use the new
* one as our dictionary from now on.
*/
public void save(ArrayList<String>
dictionary)
{
try {
FileWriter out =
new FileWriter(filename);
for(String word
: dictionary) {
out.write(word);
out.write("\n");
}
out.close();
dict =
dictionary;
}
catch(IOException exc) {
System.out.println("Error writing dictionary file: " + exc);
}
}
/**
* Load the dictionary from disk and store it in the
'dict' field.
*/
private void loadDictionary(String filename)
{
dict = new ArrayList();
try {
BufferedReader
in = new BufferedReader(new FileReader(filename));
String word =
in.readLine();
while(word !=
null) {
dict.add(word);
word = in.readLine();
}
in.close();
}
catch(IOException exc) {
System.out.println("Error reading dictionary file: " + exc);
}
}
}
The sourcecode for SpellChecker.java is given below:
package spellchecker;
import java.util.ArrayList;
import java.util.Collections;
/**
* Write a description of class SpellChecker here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SpellChecker
{
private ArrayList<String> words;
private DictReader reader;
/**
* Constructor for objects of class SpellChecker
*/
public SpellChecker()
{
reader = new
DictReader("words.txt");
words =
reader.getDictionary();
}
/**
* This method returns the number of words in the
dictionary.
* Change this.
*/
public int numberOfWords()
{
return words.size();
}
/**
* This method returns true, if (and only if) the given
word is found in the dictionary.
* Complete this.
*/
public boolean isKnownWord(String word)
{
return words.contains(word);
}
/**
* This method returns true if (and only if) all words
in the given wordList are found in the dictionary.
// Complete this.
*/
public boolean allKnown(ArrayList wordList)
{
for(String word:words) {
if(!words.contains(word))
return false;
}
return true;
}
/**
* This method tests the allKnown method. You do not
need to change this method.
*/
public boolean testAllKnown()
{
ArrayList testWords = new
ArrayList();
testWords.add("Abu");
testWords.add("Chou");
if(allKnown(testWords))
{
return
true;
}
else
{
return
false;
}
}
/**
* This method returns a list of all words from the
dictionary that start with the given prefix.
* Complete this.
*/
public ArrayList wordsStartingWith(String
prefix)
{
ArrayList<String> prefixWords
= new ArrayList<String>();
for(String word : words) {
if(word.startsWith(prefix)) {
prefixWords.add(word);
}
}
return prefixWords;
}
/**
* This method returns a list of all words from the
dictionary that include the given substring.
* Complete this.
*/
public ArrayList wordsContaining(String text)
{
ArrayList<String>
substringWords = new ArrayList<String>();
for(String word : words) {
if(word.contentEquals(text)) {
substringWords.add(word);
}
}
return substringWords;
}
/**
* Insert the given word into the dictionary.
* The word should only be inserted if it does not
already exist in the dictionary.
* If it does, the method does nothing.
* Make sure that the alphabetic order of the
dictionary is maintained.
* Complete this.
*/
public void insert(String newWord)
{
if(!words.contains(newWord))
{
int i=0;
while(i<words.size()&&words.get(i).compareTo(newWord)<0)
i++;
words.add(i,newWord);
}
}
/**
* Remove the given word from the dictionary.
* If the word was successfully removed, return
true.
* If not (for example it did not exist) return false.
Complete this.
*/
public boolean remove(String word)
{
return words.remove(word);
}
/**
* Save the dictionary to disk.
* This is not meant to be hard – there is a method in
the DictReader class that you can use.
* Complete this
*/
public void save()
{
reader.save(words);
}
}
Let me know if you find any issue in the implementation.