Question
Java Task: Implement Thesaurus class that is used from the Assign1Main.java file. The thesaurus file(synonym.txt) file looks like: stop halt cease quit discontinue explain clarify
Java
Task: Implement Thesaurus class that is used from the Assign1Main.java file.
The thesaurus file(synonym.txt) file looks like:
stop halt cease quit discontinue
explain clarify describe interpret illuminate
check see test ascertain assure
plan design strategy scheme idea
create make develop build devise
store stock shop depot put
apply use employ give implement
price cost terms toll value
number count numeral total figure
jog trot lope clip run
The thesaurus is a list of words. Each word has 4 synonyms-alternatives. As shown above.
The program should allow:
1. load a file like the synonyms.txt file
2. save the current theasaurus to a file
3. Add a new line - five words - to the thesaurus
4. Remove an entire exisitng line based on the first word.
5. Ask the thesaurus what synonyms exist for a word.
Input .txt files: You can have input .txt files in your Eclipse project. For example: if you have a file called "xyz.txt" in your src folder, you can refer to it as "src/xyz.txt" in your code to open the file. In Eclipse, the path to a file starts from the Project folder and so you don't need to know exactly where in your file system your file is.
Task: Implement the Thesaurus.java file that is used from the Assign1Main.java file.
Use a seperate class called ThesaurusLine to hold each thesaurus line as an array of 5 Strings.
Your Thesaurus class should then have
* A private data member - an array of 100 ThesaurusLine objects.
* A private data member - an int that tracks how many of the 100 locations in the array are occupied.
And these methods:
* public void loadData(String fileName) - that takes as input the name of a file and reads the contents of the file. Stroe each line of the file in a separate ThesaurusLine object and put this ojbect in the array of ThesaurusLine objects
* public void saveThesaurus(String fileName) - store all the valid ThesaurusLine objects in the array into the file named by the input string.
*public void addLine(String wordToAddtoDictionary, String synonym1, String synonym2, String synonym3, String synonym4) - create a new ThesaurusLine object from the 5 words and add it to the array data member.
*public void removeLine (String wordToRemoveFromThesaurus) - scan the array for a line where the first word matches the input and remove it from the array. Move all the ThesaurusLine objects that come after it up one place. For example, suppose we read in the above synonyms.txt file and we are asked to remove the line corresponding to "apply"; then all the remaining line - from "price" to "jog" should be moved up one spot in the array so that we have room to add one more line later.
* public String suggest (String suggestWordsFor) - scan the array for a line where the first word matches the input and return the 4 synonyms as a single String.
*Code below*
package a1;
import javax.swing.JOptionPane;
public class Assign1Main {
static final int LOAD = 1, SAVE = 2, ADD_LINE = 3, REMOVE_LINE = 4,
SUGGEST_WORD = 5, QUIT = 6;
static final String welcomeMessage =
"This program implements an interactive thesaurus. It allows you to add " +
"new lines to a thesaurus, remove lines from the thesaurus. " +
"In addition, you can load an existing thesaurus file, save the current " +
"thesaurus to a file. ";
static final String promptMessage =
"What would you like to do? Please eneter the number next to your selection "
+ LOAD + ": Load a thesaurus from a file "
+ SAVE + ": Save current thesaurus to a file "
+ ADD_LINE + ": Add a line to the current thesaurus "
+ REMOVE_LINE + ": Remove a line from the current thesaurus "
+ SUGGEST_WORD +": Suggest a synonym for a word " +
+ QUIT + ": Quit ";
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, welcomeMessage );
Thesaurus mythesaurus = new Thesaurus();
int userSelection = 0;
while (userSelection !=QUIT) {
userSelection = Integer.parseInt(JOptionPane.showInputDialog( promptMessage ) );
processSelection( mythesaurus, userSelection );
}
}
private static void processSelection(Thesaurus mythesaurus, int userSelection) {
switch(userSelection){
case LOAD:
// Remember that you can have text files in your Eclipse Project
// If this file is in src/a1, you could read the synonyms.txt file
// my file is in
String fileName = JOptionPane.showInputDialog("Please enter the name of the thesaurus file to load");
mythesaurus.loadData( fileName );
break;
case SAVE:
fileName = JOptionPane.showInputDialog("Please enter the name of the thesaurus file to save");
mythesaurus.saveThesaurus(fileName);
break;
case ADD_LINE:
String wordToAddtoThesaurus = JOptionPane.showInputDialog("Please enter the word you want to add to thesaurus");
String synonym1 = JOptionPane.showInputDialog("Please enter the first synonym for " + wordToAddtoThesaurus);
String synonym2 = JOptionPane.showInputDialog("Please enter the second synonym for " + wordToAddtoThesaurus);
String synonym3 = JOptionPane.showInputDialog("Please enter the third synonym for " + wordToAddtoThesaurus);
String synonym4 = JOptionPane.showInputDialog("Please enter the fourth synonym for " + wordToAddtoThesaurus);
mythesaurus.addLine(wordToAddtoThesaurus, synonym1, synonym2, synonym3, synonym4 );
break;
case REMOVE_LINE:
String wordToRemoveFromThesaurus = JOptionPane.showInputDialog("Please enter the word whose line you want to remove from thesaurus");
mythesaurus.removeLine(wordToRemoveFromThesaurus);
break;
case SUGGEST_WORD:
String suggestWordsFor = JOptionPane.showInputDialog("Please enter the word for which you want a synonym.");
JOptionPane.showMessageDialog(null, mythesaurus.suggest(suggestWordsFor));
break;
case QUIT:
JOptionPane.showMessageDialog(null, "Have a nice day");
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Input");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started