Homework 4.2 (submit a zip file)
Assignment 4: Dictionary
Logistics
General Description
In this assignment, you need to build a dictionary application using Swing.
The application has the following features. Users can:
add new words, new meanings to the words.
replace an old word with a new one, without changing its original meaning
and its frequency.
remove words from the dictionary and check if a word is in the dictionary.
count the frequency of searches for each word.
display the most frequent words based on the prefix they provide.
display the search history.
Import and export words in batch.
In detail, your dictionary should have the following functionalities:
Users should be able to add, remove and modify words.
Users should be able to check if a certain word is in the dictionary and
retrieve its meanings.
Users should be able to see the three most frequent words that can
complete their given keyword.
e.g., for uni, it should return universe, university, universal.
e.g., for apple, mapple, napple, lapple are feasible for the search.
The dictionary will display a search history of up to 10 words, showing the
most recently searched words first. Only the matched words (including the
three most frequent words) will be added to the search history.
Users should be able to import/export the dictionary from/into a txt file.
word0
Input format:
word_meaning0
word1
word_meaning1
…
wordn
word_meaningn
word0
Output format:
frequency0
word_meaning0
word1
frequency1
word_meaning1
…
wordn
frequencyn
word_meaningn
For export, please export the words in the (descending) order of frequency.
We ensure that no word will have the same frequency in grading test cases.
Exception Handling
In this assignment, you need handle 4 kinds of exceptions:
InvalidWordError: the word entered to add/find/clear is not a word
By mentioning word, we define it as a String consisting of a-z and A-Z
WordNotFoundError: the word is not found in our dictionary.
WordDuplicatedError: the word to be added has existed in the dictionary.
FileNotFoundError: the file path provided does not exist when importing or exporting from a txt file.
Definition of Exceptions
In order to raise those 4 exceptions, please define them by inheriting the RuntimeException class.
Once needed, you need to:
First, throw the related exception.
Then, display texts to notify that an error has occurred.
Notice:
For grading, we will test only
Layout Example for Dictionary
one error
for each exception-handling test case.
Recommended Procedure for Implementation
Create a project named Dictionary.
Create a package under src named Dictionary.
Create a form under package Dictionary with the name Dictionary.form. Check create bound class
and it will automatically create Dictionary.java
On Dictionary.form, design the GUI of Dictionary. In particular, you should (at least) have the
following components as you can see in the figure we show above:
FINDButton
Note:
only increase the frequency counts of the top 3 words.
The words feasible to be searched should include the keyword.
Display the top 3 (or 2 or 1) words in the TextFreqWord1, TextFreqWord2, TextFreqWord3
fields in the (descending) order of frequency
If there are fewer than 3 words to display, please leave those TextFreqWord fields empty.
ADDButton
MODIFYButton
Note:
replace the old word with the new one, but keep its original meaning and frequency.
REMOVEButton
CLEARButton
IMPORTButton
EXPORTButton
TextNewWord: where you type in the word you want to add/find/remove
TextOriginalWord: where you type in the frequency you want to modify
TextFreqWord1, TextFreqWord2, TextFreqWord3: 3 TextBoxes to display frequent words in finding
procedure
TextArea: where:
word meanings are displayed
error messages are displayed
searchHistoryList: a JList where show the search history
Be careful: The names of the components should be the same as showed above. Don’t forget to
consider and handle 4 self-defined exceptions during implementation!
Define 4 self-defined exceptions mentioned above using inheritance of RuntimeException.
Implement the Action Listens for all the buttons.
Test your code and see if the GUI works smoothly and has the expected functionality.
SampleTest
Please notice that SampleTest is just to help you understand the whole assignment more easily. Sample
tests are only for clearer explanation and simple testing during your implementation. Passing all the
sample tests does not mean you will get a full score. You need to read the description carefully, and
think it comprehensively when implementing this assignment.
After you complete the assignment following the recommended procedures listed above, you could the
following SampleTest code to test if your implementation works smoothly:
package Dictionary;
import java.awt.*;
public class SampleTest {
public static void main(String[] args) {
Dictionary myDictionary = new Dictionary();
// Test for ADD
// InvalidWordError
myDictionary.TextNewWord.setText(“s1mple”);
myDictionary.TextArea.setText(“Best AWPer”);
try {
myDictionary.ADDButton.doClick();
} catch (InvalidWordError ex) {
}
// WordDuplicatedError
System.out.println(“InvalidWordError passed”);
myDictionary.TextNewWord.setText(“niko”);
myDictionary.TextArea.setText(“Fortunate entry fragger”);
myDictionary.ADDButton.doClick();
myDictionary.TextNewWord.setText(“niko”);
myDictionary.TextArea.setText(“Rifler”);
try {
myDictionary.ADDButton.doClick();
} catch (WordDuplicatedError ex) {
System.out.println(“WordDuplicatedError passed”);
}
// Valid ADD
myDictionary = new Dictionary();
myDictionary.TextNewWord.setText(“niko”);
myDictionary.TextArea.setText(“Fortunate entry fragger”);
myDictionary.ADDButton.doClick();
System.out.println(“Word niko ADD successfully.”);
// Test for CLEAR button
myDictionary = new Dictionary();
myDictionary.TextNewWord.setText(“niko”);
myDictionary.TextOriginalWord.setText(“nikoo”);
myDictionary.TextFreqWord1.setText(“aaa”);
myDictionary.TextFreqWord2.setText(“bbb”);
myDictionary.TextFreqWord3.setText(“ccc”);
myDictionary.TextArea.setText(“Fortunate entry fragger”);
myDictionary.CLEARButton.doClick();
myDictionary.TextNewWord.getText();
String
String
tmp0
tmp1
=
=
myDictionary.TextOriginalWord.getText(); String tmp2 =
myDictionary.TextFreqWord1.getText(); String tmp3 =
myDictionary.TextFreqWord2.getText(); String tmp4 =
myDictionary.TextFreqWord3.getText(); String tmp5 =
myDictionary.TextArea.getText();
if (tmp0.equals(“”) && tmp1.equals(“”) && tmp2.equals(“”) && tmp3.equals(“”) && tmp4.equals(“”) &&
tmp5.equals(“”)){
System.out.println(“CLEAR button test passed”);
}
// Test for FIND button
myDictionary = new Dictionary();
// “No Word Matched.”
myDictionary.TextNewWord.setText(“NIKO”);
myDictionary.FINDButton.doClick();
if (myDictionary.TextArea.getText().equals(“No Word Matched.”)) {
System.out.println(“No Word Matched test passed”);
}
// Valid FIND Test 1
myDictionary = new Dictionary();
myDictionary.TextNewWord.setText(“niko”);
myDictionary.TextArea.setText(“Fortunate entry fragger”);
myDictionary.ADDButton.doClick();
myDictionary.CLEARButton.doClick();
myDictionary.TextNewWord.setText(“nIko”);
myDictionary.TextArea.setText(“Least Fortunate entry fragger”);
myDictionary.ADDButton.doClick();
myDictionary.CLEARButton.doClick();
myDictionary.TextNewWord.setText(“nIKO”);
myDictionary.TextArea.setText(“So-so Fortunate entry fragger”);
myDictionary.ADDButton.doClick();
myDictionary.CLEARButton.doClick();
myDictionary.TextNewWord.setText(“ni”); // return niko
myDictionary.FINDButton.doClick();
if (myDictionary.TextFreqWord1.getText().equals(“niko”)) {
System.out.println(“FIND Test 1 passed”);
}
// Valid FIND Test 2
myDictionary = new Dictionary();
myDictionary.TextNewWord.setText(“niko”);
myDictionary.TextArea.setText(“Fortunate entry fragger”);
myDictionary.ADDButton.doClick();
myDictionary.CLEARButton.doClick();
myDictionary.TextNewWord.setText(“nIko”);
myDictionary.TextArea.setText(“Least Fortunate entry fragger”);
myDictionary.ADDButton.doClick();
myDictionary.CLEARButton.doClick();
myDictionary.TextNewWord.setText(“nIKO”);
myDictionary.TextArea.setText(“So-so Fortunate entry fragger”);
myDictionary.ADDButton.doClick();
myDictionary.CLEARButton.doClick();
for (int numi = 0; numi < 3; numi++) {
// freq of “niko” is 3
myDictionary.TextNewWord.setText(“niko”);
myDictionary.FINDButton.doClick();
}
for (int numi = 0; numi < 4; numi++) {
// freq of “nIko” is 4
myDictionary.TextNewWord.setText(“nIko”);
myDictionary.FINDButton.doClick();
}
for (int numi = 0; numi < 5; numi++) {
// freq of “nIKO” is 5
myDictionary.TextNewWord.setText(“nIKO”);
myDictionary.FINDButton.doClick();
}
myDictionary.CLEARButton.doClick();
if(myDictionary.searchHistoryList.getModel().getElementAt(0).equals(“nIKO”)&&
myDictionary.searchHistoryList.getModel().getElementAt(1).equals(“nIko”)&&
myDictionary.searchHistoryList.getModel().getElementAt(2).equals(“niko”)){
System.out.println(“Search History Test passed”);
}
e l s e {
System.out.println(“Search History Test failed”);
}
myDictionary.TextNewWord.setText(“n”);
myDictionary.FINDButton.doClick();
if (myDictionary.TextFreqWord1.getText().equals(“nIKO”)
&& myDictionary.TextFreqWord2.getText().equals(“nIko”)
&& myDictionary.TextFreqWord3.getText().equals(“niko”)) {
System.out.println(“FIND Test 2 passed”);
}
// Test for REMOVE button
// test for WordNotFoundError
myDictionary = new Dictionary();
myDictionary.TextNewWord.setText(“NIKO”);
try {
myDictionary.REMOVEButton.doClick();
} catch (WordNotFoundError ex) {
System.out.println(“WordNotFoundError passed”);
}
// Valid REMOVE
myDictionary = new Dictionary();
myDictionary.TextNewWord.setText(“niko”);
myDictionary.TextArea.setText("Fortunate entry fragge