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

This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import java.util.Scanner; /** * * */ public class SoftOpening {    public static void main(String[] args) {       Scanner input = new Scanner(System.in);    ArrayList foodList = generateMenu();    User user = generateUser(input); user.introduce();    userBuyFood(foodList, user, input); user.introduce(); } public static ArrayList generateMenu(){    ArrayList foodList = new ArrayList<>();    Food pizza1 =new Food(1,"pizza","Seafood",11,12); Food pizza2 =new Food(2,"pizza","Beef",9,10); Food Friedrice =new Food(3,"fried rice","Seafood",5,12);...
This is the class that needs to be written... import java.util.ArrayList; /** * Deals out a...
This is the class that needs to be written... import java.util.ArrayList; /** * Deals out a game of 5-card-stud. * * @author PUT YOUR NAME HERE * @version PUT THE DATE HERE */ public class FiveCardStud { private Deck myDeck; private ArrayList<Hand> players; /** * Constructor for objects of class FiveCardStud */ /* Write a constructor for the FiveCardStud class. The constructor accepts a single integer parameter for the number of players. The number of players should be from 2...
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!
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process...
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process for the project */ public static void main () { System.out.println("\n\tBegin Item List Demo\n"); System.out.println("Declare an ArrayList to hold Item objects"); ArrayList<Item> list = new ArrayList<Item>(); try { System.out.println("\n Add several Items to the list"); list.add(new Item(123, "Statue")); list.add(new Item(332, "Painting")); list.add(new Item(241, "Figurine")); list.add(new Item(126, "Chair")); list.add(new Item(411, "Model")); list.add(new Item(55, "Watch")); System.out.println("\nDisplay original Items list:"); listItems(list); int result = -1; // 1....
import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for...
import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for class BirthdayList */ public static void main() { System.out.println("\n\tBegin Birthday List Program\n");    System.out.println("Declare an ArrayList for Birthday objects"); // 1. TO DO: Declare an ArrayList to store Birthday objects (3 Pts)       // 2. TO DO: Replace the xxx.xxxx() with the appropriate method (2 Pts) System.out.printf("\nBirthday List is empty: %s\n", xxx.xxxx() ? "True" : "False");    System.out.println("Add at least five Birthdays to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT