Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I understand that there is a lot of content in this question, but I am not askin

ID: 3870392 • Letter: I

Question

I understand that there is a lot of content in this question, but I am not asking for help on the entire project, just one class. Most of the information provided is needed in order for clarity of what I need help with. Thanks.

This code is meant to prompt for an input file, generate a symbol dictionary from the input file, and then write the resulting symbols to an output file, one symbol to a line. Symbols, for this project, are defined as a string of characters that do NOT include blanks, numeric, and special characters. For example, “JLabel(“Color Example”, SwingConstants.CENTER);” contains five symbols, “JLabel”, “Color”, “Example”, “SwingConstants”, and “CENTER”. All special characters should be ignored.

The necessary classes are as follows :

Class Symbol – represents symbols

Class SymbolDictionary – represents a dictionary of symbols//NEED HELP WITH WITH ENTIRE CLASS

Class SymbolIO – a class to handle all of the file IO operations that your project needs

Class Driver – A driver class

I am providing the code that I have already written, along with the UML for Symbol class and SymbolDictioary class in order to promote clarity. If you see issues with the code I have already written, help would be appreciated with that as well. Thanks for any information you can provide.

***One more possibly helpful tip, my SymbolIO class returns a String array of the tokenized file. If there is a better solution for this, I appreciate it.

SYMBOL UML

Class Symbol

INSTANCE DATA FIELDS

-strToken : String

METHODS

+Symbol(String sym)

It should throw an IllegalArgumentException with an appropriate message if an illegal symbol is attempted.

+clone()   

The clone() method should create a clone, a “deep copy”, of Symbol.

+equals(Symbol sym)

SYMBOL DICTIONARY UML

SymbolDictionary UML: Remember that duplicate symbols ARE NOT ALLOWED

//PART I NEED HELP WITH

Class SymbolDictionary

INSTANCE DATA FIELDS

Internal array(s) of symbols

The maximum number of symbols that may be stored in the array is 200.

METHODS

+findIndex(Symbol

sym) : int

Searches the array of symbols for the symbol. If the symbol is found, the method returns the index of the position of the symbol in the array. If the symbol is not found, findIndex adds the symbol to the array at the first vacant position and returns the index of that position.

+isPresent(Symbol sym) : boolean

Returns true or false, corresponding to whether or not the symbol is present in the dictionary.

+remove(Symbol sym) : void

Removes the symbol from the dictionary, leaving the index of the symbol unused (i.e., this creates a vacant position in the array). This method takes no action if the symbol is not present.

MY CODE:

Symbol Class

public class Symbol{

     

            String sym;

            Symbol(String sym){

            this.sym=sym;

            }

            @Override

            public String toString() {

            return sym;

            }

           

            public Object clone(){

                  Symbol s;

                 

                  try{

                        s = (Symbol) super.clone();

                  }

                  catch(CloneNotSupportedException e){

                        return null;

                  }

                  return s;

            }

           

            public boolean equals(Symbol sym){

                  Symbol s;

                  if(sym instanceof Symbol){

                        s = (Symbol) sym;

                        return true;

                  }

                  else{

                        return false;

                  }

            }

            }

SymbolIO Class

import java.util.Scanner;

import java.util.regex.Pattern;

import java.io.File;

import java.io.FileNotFoundException

public class SymbolIO {

      Scanner sc = null;

      Pattern pattern = Pattern.compile("[^\p{Alpha}']+");

      String str;

      File inputFile;

      String[] symbolArr = new String [200];

     

      public SymbolIO(String name){

            inputFile = new File(name);

      }

     

      public String[] readFile(File file){

            try{

                  sc = new Scanner(file);

            }

            catch(FileNotFoundException e){

                  System.out.println("Not Found");

            }

           

            int i =0;

            while(sc.hasNext()){

                  sc.useDelimiter(pattern);

                  str = sc.next();

                  if(!str.equals("")){

                        symbolArr[i] = str;

                       

                  }

                  i++;

            }

            return symbolArr;

}}

  

Class Symbol

INSTANCE DATA FIELDS

-strToken : String

METHODS

+Symbol(String sym)

It should throw an IllegalArgumentException with an appropriate message if an illegal symbol is attempted.

+clone()   

The clone() method should create a clone, a “deep copy”, of Symbol.

+equals(Symbol sym)

Explanation / Answer

I have modified your existing class and created the new classes needed. Hope the answer helped. In case of any issues , post a comment. If the answer helped, please do rate it. Thank you

Symbol.java

public class Symbol{

String strToken;

Symbol(String sym){

this.strToken=sym;

}

@Override

public String toString() {

return strToken;

}

public Object clone(){

Symbol s;

try{

s = (Symbol) super.clone();

s.strToken = strToken;

}

catch(CloneNotSupportedException e){

return null;

}

return s;

}

public boolean equals(Symbol sym){

   return strToken.equals(sym.strToken);

}

   }

SymbolIO.java

import java.util.Scanner;

import java.util.regex.Pattern;

import java.io.File;

import java.io.FileNotFoundException;

public class SymbolIO {

private Scanner inputFile = null;

Pattern pattern = Pattern.compile("[^\p{Alpha}']+"); //1 or more non alphabet chars

public SymbolIO(String name) throws FileNotFoundException {

inputFile = new Scanner(new File(name));

inputFile.useDelimiter(pattern);

  

}

  

public boolean hasNext()

{

      return inputFile.hasNext();

}

  

public Symbol next()

{

      String sym = inputFile.next();

      return new Symbol(sym);

}

  

public void close()

{

      inputFile.close();

}

}

SymbolDictionary.java

public class SymbolDictionary {

   private Symbol[] symbols;

   public SymbolDictionary()

   {

       symbols = new Symbol[200];

   }

  

   int findIndex(Symbol sym)

   {

       int vacant = -1;

       for(int i = 0; i < symbols.length; i++)

       {

           if(symbols[i] == null)

           {

               if(vacant == -1)

                   vacant = i; //save the first vacant index in case its needed if symbol was not found

           }

           else

           {

               if(symbols[i].equals(sym))

                   return i;

           }

       }

      

       //not returned any index from loop above , so symbol not found,

       //addd it to vacant position

      

       if(vacant != -1) //there is some vacant position

           symbols[vacant] = sym;

      

       return vacant;

      

   }

  

   public boolean isPresent(Symbol sym)

   {

       for(int i =0 ; i < symbols.length; i++)

           if(symbols[i].equals(sym))

               return true;

      

       //not found

       return false;

   }

  

   public void removeSymbol(Symbol sym)

   {

       for(int i =0 ; i < symbols.length; i++)

           if(symbols[i].equals(sym))

           {

               symbols[i] = null; //make it vacant

               break;

           }

      

   }

  

   public String toString()

   {

       String str = "" ;

       for(int i = 0; i < symbols.length; i++)

       {

           if(symbols[i] != null)

               str += symbols[i] + " ";

       }

       return str;

   }

  

}

Driver.java

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class Driver {

   public static void main(String[] args) {

       Scanner keybd = new Scanner(System.in);

       String inFile, outFile;

       System.out.print("Enter input filename: ");

       inFile = keybd.nextLine().trim();

      

       System.out.print("Enter output filename: ");

       outFile = keybd.nextLine().trim();

      

       try {

           SymbolIO symIO = new SymbolIO(inFile);

           SymbolDictionary dict = new SymbolDictionary();

           Symbol s;

           while(symIO.hasNext())

           {

               s = symIO.next();

               dict.findIndex(s); //if not present , will add it in vacant position

           }

          

           symIO.close();

          

           //Write teh symbols to output file

           PrintWriter pw = new PrintWriter(outFile);

           pw.write(dict.toString());

           pw.close();

          

           System.out.println("Symbols written to " + outFile);

       } catch (FileNotFoundException e) {

           System.out.println(e.getMessage());

       }

      

   }

}

input file: syminput.txt

“JLabel(“Color Example”, SwingConstants.CENTER);”

output file: symoutput.txt

JLabel
Color
Example
SwingConstants
CENTER