Java Do not use any built in functions such as ArrayList pub

Java. Do not use any built in functions such as ArrayList

public class SPC{

// A class to do spell checking. This version only marks misspelled terms with

// asterisks as in **mispeling**. It serves as a parent class.

protected String [] dictionaryTerms;    

// Array of terms considered correct by spell checker

protected boolean disregardCase;

// If this is true, ignore case when checking the spelling of terms;      otherwise

// capitalization differences will be counted as misspellings

public SPC(String dictionaryFN, boolean disregardCase);

// make a checker that checks the spelling of words. dictionaryFN is the name of a file containing all

// terms that are considered correct, one on each line; english-dict.txt is

// commonly used. disregardCase indicates whether case should be ignored or used

// when checking for term correctness against dictionary terms.

public int dictionarySize();

// Return the size of the dictionary used by this checker which is the

// number of terms read from the dictionary file and stored in the

// dictionaryTerms array.

public boolean isValid(String term);

// Return true if the provided term is considered correct by the spell checker

// and false otherwise. A term is correct if it is equal to a term in the

// dictionaryTerm array. It is also correct if case is being ignored and is

// equal ignoring case to some term in the dictTerms array.

}

Solution

Since the input file english-dict.txt has not been provided, I could not provide the OUTPUt here.
However the code works fine. I have checked and verified the same. If you face any problems, please let me know back,
I will help you with it.

CODE:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class SPC {
   // variables declared here.
   protected String [] dictionaryTerms;
   protected boolean disregardCase;
  
   // constructor
   public SPC(String dictionaryFN, boolean disregardCase) throws FileNotFoundException{
      
       // trying to open the file here using the Scanner class.
       File fp = new File(dictionaryFN);
       Scanner sc = new Scanner(fp);
       int i =0;
      
       // read every line and feed it into the dictionaryTerms array.
       while(sc.hasNextLine()){
           dictionaryTerms[i] = sc.nextLine();
           i++;
       }
      
       // close the scanner class.
       sc.close();
      
       // store the case option passed on here.
       this.disregardCase = disregardCase;
   }
  
   // returns the length of the dictionaryTerms
   public int dictionarySize(){
       return dictionaryTerms.length;
   }
  
   // checks if the input term is valid.
   public boolean isValid(String term){
  
       // if case of the term is not to be considered, then ignore the case while comparing.
       if(disregardCase){
           for(int i =0; i<dictionarySize();i++){
               if (dictionaryTerms[i].toLowerCase() == term.toLowerCase())
                   return true;
           }
           return false;
       }
       // if case to be considered, then DO NOT ignore the case while comparing.
       else{
           for(int i =0; i<dictionarySize();i++){
               if (dictionaryTerms[i] == term)
                   return true;
           }
           return false;
       }
   }
  
  
   public static void main(String[] args) throws FileNotFoundException{
  
       // please provide the complete path to the file as in your system.
       String filename = \"pathTo english-dict.txt\";
      
       // call the constructor with file name and case value.
       SPC sp = new SPC(filename,false);
      
       // please provide the term to check.
       String term = \" term to check \";
      
       // this will output if the term is valid.
       System.out.println(\"Is the term valid ?? \" + sp.isValid(term));
   }
}

Java. Do not use any built in functions such as ArrayList public class SPC{ // A class to do spell checking. This version only marks misspelled terms with // as
Java. Do not use any built in functions such as ArrayList public class SPC{ // A class to do spell checking. This version only marks misspelled terms with // as
Java. Do not use any built in functions such as ArrayList public class SPC{ // A class to do spell checking. This version only marks misspelled terms with // as

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site