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

JAVA PROGRAMMING write a dictionary program that uses a tree structure. Next hav

ID: 3771333 • Letter: J

Question

JAVA PROGRAMMING

write a dictionary program that uses a tree structure.

Next have the user provide the name of a text (*.txt) file with a dictionary of English words in it.

Next it is to read in the dictionary of words from the input text file. Make certain to have this in a try block in case a bad file name was input. If the program cannot open the file for input, it must prompt the user again for a file name and continue to do this until a good name is provided. The file is to have one word on each line and may be of any length.

You must reject (ignore and display an error message) any words being input that have non-letters in them, that is,something like all13 or f@t. So no hyphenated words. But your program is to continue running after displaying the error messages as if nothing was wrong except that the rejected words will not be placed in the dictionary.

All acceptable words input should be converted so that all letters are in lower case.

The program is to then create a tree structure from the input words.

I would suggest using a linked list with up to 26 branches at each node for this, but you may use some other treestructure if you wish.

The program will then loop asking the user to input a word which the program will check to see if it is in the dictionary or not using the created tree. If the word is in the dictionary, the program will display the message: “xxxxx is in the dictionary” where xxxxx is the input word. If the word is not found in the dictionary, the program is to display the message: “xxxxx is not in the dictionary”. In either event, the program is to then ask for another word to be input and check if it is in the dictionary or not. This will continue until an empty string is input which will terminate the program.

Note, you must make certain that your program can handle words like the following in the dictionary: “a”, “an”, “ant”, “another” etc. and successfully find them if they are input. You may want to use a flag indicating that a node may be the end of a word, but that additional nodes may follow because another word (or words) has the same start but more letters.

Explanation / Answer


import java.util.Scanner;   

import java.io.IOException; // for IOException
import java.util.ArrayList; // Used to create ArrayLists
import java.io.*; // Used for IOException, File

// Declare the class
public class Dictionary
{
// Declare a dynamically allocated ArrayList of Strings for the dictionary.
// The dictionary can hold any number of words.
ArrayList<String> dictionary;
  
  
/** Dictionary() Constructor
* Default constructor
*/
public Dictionary() throws IOException
{
// First take care of creating and initializing the dictionary
// Define the instance of the dictionary ArrayList
dictionary = new ArrayList<String>();
// Now fill the dictionary array list with words from the dictionary file
readInDictionaryWords();
}//end Dictionary constructor
  
  
/** readInDictionaryWords()

public void readInDictionaryWords() throws IOException
{
// Define a Scanner to read from an input file. Note that the name of
// the file given in the code below MUST match the actual filename of
// the dictionary file. This file should be in the same directory
// as the source code for WordCross.java
File dictionaryFile = new File("dictionary.txt"); // declare the file
// ensure file exists and is in the correct directory
if( ! dictionaryFile.exists()) {
System.out.println("*** Error *** " +
"Your dictionary file has the wrong name or is " +
"in the wrong directory. " +
"Aborting program... ");
System.exit( -1); // Terminate the program
}
Scanner inputFile = new Scanner( dictionaryFile);
  
// while there are words in the input file, add them to the dictionary,
// converting them to upper case
while( inputFile.hasNext()) {
dictionary.add( inputFile.nextLine().toUpperCase() );
}
}//end readInDictionaryWords()
  
  
/** wordExists()
* Allow looking up a word in dictionary, returning a value of
*/
public boolean wordExists( String wordToLookup)
{
if( dictionary.contains( wordToLookup)) {
return true; // words was found in dictionary
}
else {
return false; // word was not found in dictionary
}
}/
  
  
}

Thank you