hello i was needed some help on step by step to make sure I am doing this right,
ID: 3537194 • Letter: H
Question
hello i was needed some help on step by step to make sure I am doing this right, here;s the hw question with the bluej file below
The goal of this assignment is to implement a class that serves as a spellchecker for a text application (for example, a word processor, or something similar).
You are given a dictionary file that lists the words in the English language (or at least an arbitrary subset) and the source code for another class called DictReader. This class can read in the dictionary and provide it as an ArrayList of Strings. You will complete the SpellChecker class.
Complete the following methods in the SpellChecker class:
Make sure that your wordsStartingWith and wordsContaining methods are case-insensitive. That means, for instance, that looking up words starting with %u201Cgilb%u201D should find %u201CGilbert%u201D.
here's some of the notes added to the bluej file ::::
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.
*
*/
public int numberOfWords()
{
return 4;
}
/**
* This method returns true, if (and only if) the given word is found in the dictionary.
*
*/
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.
* ==(( write a allKNown method))==
*
*/
public boolean allKnown(ArrayList<String> wordList)
{
return false;
}
/**
* This method tests the allKnown method.
*/
public boolean testAllKnown()
{
ArrayList<String> testWords = new ArrayList<String>();
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.
*==((create a local variable))==
*/
public ArrayList<String> wordsStartingWith(String prefix)
{
return null;
}
/**
* This method returns a list of all words from the dictionary that include the given substring.
*==((contains string method ))==
*/
public ArrayList<String> 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. ======//(( isKNownWord.. if its not alrady in their use add method... sort by static method in== Collections.sort(words); )//
* If it does, the method does nothing.
* Make sure that the alphabetic order of the dictionary is maintained.
*
*/
public void insert(String newWord)
{
}
/**
* Remove the given word from the dictionary. ==(( remove method))==
* If the word was successfully removed, return true.
* If not (for example it did not exist) return false.==(( use isKnownWord ))==
*/
public boolean remove(String word)
{
if (words.remove(word))
{
return true;
}
return false;
}
/**
* Save the dictionary to disk. ==(( getDictionary , Class is declared /// use object from other class(reader) ))==
* This is not meant to be hard %u2013 there is a method in the DictReader class that you can use.
*
*/
public void save()
{
reader.save(words);
}
}
Explanation / Answer
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.
*
*/
public int numberOfWords()
{
return words.size();
}
/**
* This method returns true, if (and only if) the given word is found in the dictionary.
*
*/
public boolean isKnownWord(String word)
{
for(int i=0;i<numberOfWords();i++)
{
if(word.compareToIgnoreCase(words.get(i))==0)
return true;
}
return false;
}
/**
* This method returns true if (and only if) all words in the given wordList are found in the dictionary.
* ==(( write a allKNown method))==
*
*/
public boolean allKnown(ArrayList<String> wordList)
{
int flag=0;
for(int j=0;j<wordList.size();j++)
{
flag=0;
for(int i=0;i<numberOfWords();i++)
{
if(wordList.get(j).compareToIgnoreCase(words.get(i))==0)
{ flag=1; break; }
}
if(flag!=1) { return false; }
}
return true;
}
/**
* This method tests the allKnown method.
*/
public boolean testAllKnown()
{
ArrayList<String> testWords = new ArrayList<String>();
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.
*==((create a local variable))==
*/
public ArrayList<String> wordsStartingWith(String prefix)
{
ArrayList<String> pWords = new ArrayList<String>();
for(int i=0;i<numberOfWords();i++)
{
if(words.get(i).startsWith(prefix))
pWords.add(words.get(i));
}
return pWords;
}
/**
* This method returns a list of all words from the dictionary that include the given substring.
*==((contains string method ))==
*/
public ArrayList<String> wordsContaining(String text)
{
ArrayList<String> pWords = new ArrayList<String>();
for(int i=0;i<numberOfWords();i++)
{
String searchMe = words.get(i);
int searchMeLength = searchMe.length();
int findMeLength = text.length();
boolean foundIt = false;
for (int j = 0;
j <= (searchMeLength - findMeLength);
j++) {
if (searchMe.regionMatches(j, text, 0, findMeLength)) {
foundIt = true;
pWords.add(searchMe);
break;
}
}
}
return pWords;
}
/**
* Insert the given word into the dictionary.
* The word should only be inserted if it does not already exist in the dictionary. ======//(( isKNownWord.. if its not alrady in their use add method... sort by static method in== Collections.sort(words); )//
* If it does, the method does nothing.
* Make sure that the alphabetic order of the dictionary is maintained.
*
*/
public void insert(String newWord)
{
if(!isKnownWord(newWord))
{
words.add(newWord);
Collections.sort(words);
}
}
/**
* Remove the given word from the dictionary. ==(( remove method))==
* If the word was successfully removed, return true.
* If not (for example it did not exist) return false.==(( use isKnownWord ))==
*/
public boolean remove(String word)
{
if(!isKnownWord(word))
{ return false; }
if (words.remove(word))
{
return true;
}
return false;
}
/**
* Save the dictionary to disk. ==(( getDictionary , Class is declared /// use object from other class(reader) ))==
* This is not meant to be hard %u2013 there is a method in the DictReader class that you can use.
*
*/
public void save()
{
reader.save(words);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.