Write a menu driven program that either accepts words and their meanings, or dis
ID: 3817928 • Letter: W
Question
Write a menu driven program that either accepts words and their meanings, or displays the list of words in lexicographical order (i.e. as in a dictionary). When an entry is to be added to the dictionary you must first enter the word as one string, and then enter the meaning as separate string. A word may have more than one meaning, and may be entered at separate times. When this occurs, place each successive meaning on a separate line. This new meaning must be preceded by a dash. For example, if you enter the following words and with their meanings in the following order: Library, School, Cup, and School, then your output should be a display as shown in the Figure 1.
Figure 1.
Another requirement - from time to time words become obsolete. When this happens, such word must be removed from the dictionary.
Use the JOptionPane class to enter the information.
Use the concept of linked list to carryout this exercise. You will need at minimum the following classes:
A WordMeaning class that hold the name of a word and its meaning.
A WordMeaningNode class that creates the node of information and its link field.
A WordList class that creates and maintain a linked list of words and their meanings.
A Dictionary class that test your classes.
For the output, the program should produce two scrollable lists:
The current list of words and their meanings, and
The list of the deleted words. You need not list the meanings, just the words.
NB. The linked list must be designed from first principle. Do not use the pre-defined class LinkedList found in java.util; nor array; nor ArrayList class.
Explanation / Answer
WordMeaning Class:
public class WordMeaning {
String name;
String definition;
WordMeaning(String t, String d) {
name = t;
definition = d;
}
String getName() {
return name;
}
String getDefinition() {
return definition;}
}
}
WordMeaningNode Class:
public class WordMeaningNode {
WordMeaning wordMeaning;
WordMeaningNode next;
WordMeaningNode(WordMeaning w) {
wordMeaning = w;
next = null;
}
public WordMeaning getWordMeaning()
{
return wordMeaning;
}
}
WordList Class:
public class WordList {
WordMeaningNode list;
WordList() {
list = null;
}
void add(WordMeaning w)
{
WordMeaningNode temp = new WordMeaningNode(w);
if (list == null)
list = temp;
else
{
WordMeaningNode aux = list;
WordMeaningNode back = null;
boolean found = false;
while(aux != null && !found)
if( temp.getWordMeaning().getName().compareTo(aux.getWordMeaning().getName()) < 0 )
found = true;
else
{
back = aux;
aux = aux.next;
}
temp.next = aux;
if (back == null)
list = temp;
else
back.next = temp;
}
}
boolean listIsEmpty() {
boolean empty;
if (list == null) {
empty = true;
} else {
empty = false;
}
return empty;
}
public String toString()
{
String result = "";
int count = 0;
WordMeaningNode current = list;
while (current != null)
{
count++;
result += current.getWordMeaning().getName() + " " + " " + current.getWordMeaning().getDefinition();
current = current.next;
}
return result + " The number of words is : " + count;
}
}
Dictionary Class:
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
class Dictionary {
public static void main(String[] args) {
WordMeaning entry;
WordList diction = new WordList();
WordList obsolete = new WordList();
boolean more = true;
int menuOption = 0;
String menuMessage = "1. Enter a word and its definition 2. Remove a"
+ "word 3. Display all words and meanings 4. Display"
+ "Removed words "
+ "Enter the Menu Option (1,2,3, or 4--Any other key"
+ "exits the program)";
do {
menuOption = GetData.getInt(menuMessage);
switch (menuOption) {
case 1:
String word = GetData.getString("Enter the word to define:"+ " ").toUpperCase();
String meaning = GetData.getString("Enter the meaning of "+ word + " : ");
meaning = " - " + meaning;
entry = new WordMeaning(word, meaning);
diction.add(entry);
JOptionPane.showMessageDialog(null, word + " was added to"+ " the dictionary.", "New Entry",
JOptionPane.INFORMATION_MESSAGE);
break;
case 2:
word = GetData.getString("Enter the obsolete word:")
.toUpperCase();
try {
diction.remove(word);
obsolete.add(new WordMeaning(word, " "));
JOptionPane.showMessageDialog(null, word + "has been removed!", "Word Removal", JOptionPane.INFORMATION_MESSAGE);
} catch (NullPointerException e) {
JOptionPane.showMessageDialog(null, word + "does not exist", "Word Removal", JOptionPane.INFORMATION_MESSAGE);
}
break;
case 3:
JTextArea text = new JTextArea(diction.toString(), 10, 40);
JScrollPane pane = new JScrollPane(text);
JOptionPane.showMessageDialog(null, pane, "Current"+ "Dictionary",JOptionPane.INFORMATION_MESSAGE);
break;
case 4:
text = new JTextArea(obsolete.toString(), 10, 40);
pane = new JScrollPane(text);
JOptionPane.showMessageDialog(null, pane, "Obsolete Words",JOptionPane.INFORMATION_MESSAGE);
break;
default:
more = false;
}
} while (more);
}
}
GetData Class:
import javax.swing.JOptionPane;
class GetData
{
static double getDouble(String s)
{
return Double.parseDouble(getString(s));
}
static int getInt(String s)
{
return Integer.parseInt(getString(s));
}
static String getString(String s)
{
return JOptionPane.showInputDialog(s);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.