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

AVA PROGRAMMING write a dictionary program that uses a tree structure. Next have

ID: 3771599 • Letter: A

Question

AVA 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

//Program: using Netbeans IDE

// contains class Node, class Dictionary, class DictionaryDriver (main )

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

class Node
{
String word;
String meaning;
public Node next;
public Node()
{
word="";
meaning="";
next=null;
}
public Node(String w, String m)
{
word=w;
meaning=m;
next=null;
}
public String getWord()
{
return word;
}
public String getMeaning()
{
return meaning;
}

}
class Dictionary
{
Node root[];
public Dictionary()
{
root=new Node[26];
for(int i=0;i<26;i++)
{
root[i]=null;
}
}
public void CreateDictionary(String file)
{
try{
BufferedReader br = new BufferedReader(new FileReader(file));
if(br==null)
{
System.out.println("File cannot be read");
System.exit(0);
}
String line = "";
StringTokenizer st = null;
//read white space separated file line by line
while ((line = br.readLine()) != null)
{
st = new StringTokenizer(line, " ");
String word=st.nextToken().toLowerCase();
String meaning=st.nextToken().toLowerCase();
int alpha=isWord(word);
if(alpha==-1)
{
System.out.println(word +" is not a proper word. Rejected!!");
}
else
{
insertWord(alpha,word,meaning);
}
}
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}

private int isWord(String word) {
int alpha=-2;
char a;
char alphabets[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char letters[]=word.toCharArray();
for(int i=0;i<letters.length;i++)
{
for(int j=0;j<26;j++)
{
if((letters[i]!=alphabets[j]))
{
alpha=-1;
break;
}
}
}
if(alpha!=-1)
{
for(int j=0;j<26;j++)
{
if((letters[0]==alphabets[j]))
{
alpha=j;
break;
}
}
}
return alpha;
}

private void insertWord(int alpha, String word, String meaning) {
if(root[alpha]==null)
{
root[alpha]=new Node(word,meaning);
root[alpha].next=null;
}
else
{
Node p=root[alpha];
while(p!=null)
{
p=p.next;
}   
p.next=new Node(word,meaning);
}
}
public String Search(String Name)
{
String meaning="";
int alpha=isWord(Name);
if(alpha>=0)
{
Node ptr=root[alpha];
while(ptr!=null)
{
if(ptr.word.equalsIgnoreCase(Name))
{
meaning=ptr.meaning;
break;
}
ptr=ptr.next;
}   
}
return meaning;
}
  
}
public class DictionaryDriver {
  
public static void main(String[] args) {
Dictionary D=new Dictionary();
System.out.println("Enter file name which contains words");
Scanner sc=new Scanner(System.in);
String file=sc.next();

D.CreateDictionary(file);
while(true)
{
System.out.println("Enter a word to search in dictionary");
String word=sc.next();
String meaning=D.Search(word);
if(meaning.equals(""))
System.out.println(word +"is not available in dictionary");
else
System.out.println(word +"is present and its meaning is "+meaning);
System.out.println("Do you want to search another word? <1/0>");
int ch=sc.nextInt();
if(ch==0)
break;
}
  
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote