Question

In: Computer Science

This is the code that needs to be completed... import java.util.ArrayList; import java.util.Collections; /** * Write...

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);
}
}
}

Solutions

Expert Solution

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.


Related Solutions

Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random;...
Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random; public class Main { public static void main(String[] args) { Character char1 = new Orc("Grumlin"); Character char2 = new Elf("Therae"); int damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage"); char1.hasCastSpellSkill = true; damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage");...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public static void main(String[] args)    {        max_cards=45;        arr->new ArraryList        col=1;        card=0;        left=max_cards;        do{            col->random number            row->new ArrayList;            for i=0 to i<col            {                card++                add card into row            }   ...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public class CourseeCOM616 {        private String courseName;     private String[] studentsName = new String[1];     private String studentId;        private int numberOfStudents;        public CourseeCOM616(String courseName) {         this.courseName = courseName;     }     public String[] getStudentsName() {         return studentsName;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getStudentId() {         return studentId;    ...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
Abstract Cart class import java.util.ArrayList; import java.util.HashMap; /** * The Abstract Cart class represents a user's...
Abstract Cart class import java.util.ArrayList; import java.util.HashMap; /** * The Abstract Cart class represents a user's cart. Items of Type T can be added * or removed from the cart. A hashmap is used to keep track of the number of items * that have been added to the cart example 2 apples or 4 shirts. * @author Your friendly CS Profs * @param -Type of items that will be placed in the Cart. */ public abstract class AbstractCart {...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT