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

Use your understanding of enum and different classes if possible We are hired to

ID: 3749606 • Letter: U

Question

Use your understanding of enum and different classes if possible

We are hired to implement an interactive dictionary. Our dictionary takes input from users and uses the input as search key to look up values associated with the key. Requirements: Coding: No hard coding, https://en.wikipedia.org/wiki/Hard coding Data Source: Store the original data in a set of enum objects. Each keyword, each part of speech, and each definition must be stored in a separate data field. Do not combine them such as storing three parts in one String Data Structure: Use existing data structure(s) or creating new data structure(s) to store our dictionary's data. Data Loading: When our program starts, it loads all the original data from the enum objects into our dictionary's data structure User Interface: A program interface allows users to input search keys and displays returned results. Our program searches the dictionary's data (not the enum objects) for values associated with the search keys Identical Output: Our program's output must be identical to the complete sample run's output. The complete output is -

Explanation / Answer

//Dictionary implemented using a Trie Tree.

public class Dictionary {

private HashMap<Character,Node> roots = new HashMap<Character,Node>();

/**

* Search through the dictionary for a word.

* @param string The word to search for.

* @return Whether or not the word exists in the dictionary.

*/

public boolean search(String string) {

if (roots.containsKey(string.charAt(0))) {

if (string.length()==1) {

return true;

}

return searchFor(string.substring(1),roots.get(string.charAt(0)));

} else {

return false;

}

}

/**

* Insert a word into the dictionary.

* @param string The word to insert.

*/

public void insert(String string) {

if (!roots.containsKey(string.charAt(0))) {

roots.put(string.charAt(0), new Node());

}

insertWord(string.substring(1),roots.get(string.charAt(0)));

}

//Adds a new word to the trie tree.

private void insertWord(String string, Node node) {

final Node nextChild;

if (node.children.containsKey(string.charAt(0))) {

nextChild = node.children.get(string.charAt(0));

} else {

nextChild = new Node();

node.children.put(string.charAt(0), nextChild);

}

if (string.length() == 1) {

nextChild.endOfWord = true;

return;

} else {

insertWord(string.substring(1),nextChild);

}

}

//Recursive method that searches through the Trie Tree to find the value.

private boolean searchFor(String string, Node node) {

if (string.length()==0) {

if (node.endOfWord) {

return true;

} else {

return false;

}

}

if (node.children.containsKey(string.charAt(0))) {

return searchFor(string.substring(1),node.children.get(string.charAt(0)));

} else {

return false;

}

}

}

Node.java

publicclass Node {

public Node parent;

public Boolean endOfWord = false; //Does this Node mark the end of a particular word?

public HashMap<Character,Node> children = new HashMap<Character,Node>();

}

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