Question: Please help to correct the output order for distinct distinct Search[9], reverse distinct Search[16], reverse distinct reverse Search[17], reverse noun distinct ok Search[21], and reverse
Please help to correct the output order for "distinct distinct" Search[9], "reverse distinct" Search[16], "reverse distinct reverse" Search[17], "reverse noun distinct ok" Search[21], and "reverse distinct reverse ok" Search[24].
package dictionary;
import java.util.*;
public class Dictionary {
EnumSet
boolean foundPOS = false;
boolean foundPOSIsDistinct = false;
boolean foundKeyWord = false;
public enum DictionaryWord {
ADJECTIVE,
INTERJECTION,
DISTINCT,
PLACEHOLDER,
REVERSE,
ARROW,
PRONOUN,
BOOK,
ADVERB,
CONJUNCTION,
PREPOSITION,
NOUN,
VERB;
private String[] noun = null;
private String[] adverb = null;
private String[] adjective = null;
private String[] conjuction = null;
private String[] pronoun = null;
private String[] verb = null;
private String[] preposition = null;
private String[] interjection = null;
private boolean pos = false;
private boolean param = false;
public boolean isPOS() {
return pos;
}
public String[] getNoun() {
return noun;
}
public Set getDistinctNoun() {
if (noun != null) {
return new HashSet(Arrays.asList(noun));
}
return null;
}
public void setNoun(String[] noun) {
this.noun = noun;
}
public String[] getAdverb() {
return adverb;
}
public Set getDistinctAdverb() {
if (adverb != null) {
return new HashSet(Arrays.asList(adverb));
}
return null;
}
public void setAdverb(String[] adverb) {
this.adverb = adverb;
}
public String[] getAdjective() {
return adjective;
}
public Set getDistinctAdjective() {
if (adjective != null) {
return new HashSet(Arrays.asList(adjective));
}
return null;
}
public void setAdjective(String[] adjective) {
this.adjective = adjective;
}
public String[] getConjuction() {
return conjuction;
}
public Set getDistinctConjuction() {
if (conjuction != null) {
return new HashSet(Arrays.asList(conjuction));
}
return null;
}
public void setConjuction(String[] conjuction) {
this.conjuction = conjuction;
}
public String[] getPronoun() {
return pronoun;
}
public Set getDistinctPronoun() {
if (pronoun != null) {
return new HashSet(Arrays.asList(pronoun));
}
return null;
}
public void setPronoun(String[] pronoun) {
this.pronoun = pronoun;
}
public String[] getVerb() {
return verb;
}
public Set getDistinctVerb() {
if (verb != null) {
return new HashSet(Arrays.asList(verb));
}
return null;
}
public void setVerb(String[] verb) {
this.verb = verb;
}
public String[] getPreposition() {
return preposition;
}
public Set getDistinctPreposition() {
if (preposition != null) {
return new HashSet(Arrays.asList(preposition));
}
return null;
}
public void setPreposition(String[] preposition) {
this.preposition = preposition;
}
public String[] getInterjection() {
return interjection;
}
public Set getDistinctInterjection() {
if (interjection != null) {
return new HashSet(Arrays.asList(interjection));
}
return null;
}
public void setInterjection(String[] interjection) {
this.interjection = interjection;
}
public boolean isPos() {
return pos;
}
public void setPos(boolean pos) {
this.pos = pos;
}
public boolean isParam() {
return param;
}
public void setParam(boolean param) {
this.param = param;
}
}
public void loadData() {
System.out.print("! Loading data... ");
DictionaryWord arrow = DictionaryWord.ARROW;
arrow.setNoun(new String[]{"Here is one arrow: -=>> "});
arrow.setPos(false);
DictionaryWord distinct = DictionaryWord.DISTINCT;
distinct.setParam(false);
distinct.setAdjective(new String[]{"Familiar. Worked in Java",
"Unique. No duplicates. Clearly different or of a different kind."});
distinct.setAdverb(new String[]{
"Uniquely. Written \"distinctly\""});
distinct.setNoun(new String[]{"A keyword in this assignment.",
"A keyword in this assignment.", "A keyword in this assignment.",
"An advanced search option", "Distinct is a parameter in this assignment."});
DictionaryWord placeholder = DictionaryWord.PLACEHOLDER;
placeholder.setPos(false);
placeholder.setAdjective(new String[]{"To be updated...", "To be updated..."});
placeholder.setAdverb(new String[]{"To be updated..."});
placeholder.setConjuction(new String[]{"To be updated..."});
placeholder.setInterjection(new String[]{"To be updated..."});
placeholder.setNoun(new String[]{"To be updated...", "To be updated...", "To be updated..."});
placeholder.setPreposition(new String[]{"To be updated..."});
placeholder.setPronoun(new String[]{"To be updated..."});
placeholder.setVerb(new String[]{"To be updated..."});
DictionaryWord reverse = DictionaryWord.REVERSE;
reverse.setPos(false);
reverse.setAdjective(new String[]{"On back side.", "Opposite to usual or previous arrangement."});
reverse.setNoun(new String[]{"A dictionary program's parameter.", "Change to opposite direction.",
"The opposite.", "To be updated...", "To be updated...", "To be updated...", "To be updated..."});
reverse.setVerb(new String[]{"Change something to opposite.", "Go back", "Revoke ruling.", "To be updated...",
"To be updated...", "Turn something inside out."});
DictionaryWord book = DictionaryWord.BOOK;
book.setPos(false);
book.setNoun(new String[]{"A set of pages.", "A written work published in printed or electronic form."});
book.setVerb(new String[]{"To arrange for someone to have a seat on a plane.",
"To arrange something on a particular date."});
DictionaryWord adverb = DictionaryWord.ADVERB;
adverb.setPos(true);
adverb.setNoun(new String[]{"adverb is a word that adds more information"
+ " about place, time, manner, cause or degree to a verb, an adjective, a phrase or another adverb."});
DictionaryWord adjective = DictionaryWord.ADJECTIVE;
adjective.setPos(true);
adjective.setNoun(new String[]{"adjective is a word that describes a person or thing,"
+ " for example big, red and clever in a big house, red wine and a clever idea."});
DictionaryWord conjuction = DictionaryWord.CONJUNCTION;
conjuction.setPos(true);
DictionaryWord interjection = DictionaryWord.INTERJECTION;
interjection.setPos(true);
interjection.setNoun(new String[]{"interjection is a short sound, word or phrase"
+ " spoken suddenly to express an emotion. Oh!, Look out! and Ow! are interjections"});
DictionaryWord noun = DictionaryWord.NOUN;
noun.setPos(true);
noun.setNoun(new String[]{"noun is a word that refers to a person, "
+ "(such as Ann or doctor), a place (such as Paris or city) or a "
+ "thing, a quality or an activity (such as plant, sorrow or tennis"});
DictionaryWord preposition = DictionaryWord.PREPOSITION;
preposition.setPos(true);
DictionaryWord verb = DictionaryWord.VERB;
verb.setPos(true);
DictionaryWord pronoun = DictionaryWord.PRONOUN;
pronoun.setPos(true);
//Load data from Data Source to EnumSet Data Structure
dataStructure = EnumSet.of(arrow, distinct, placeholder, reverse, book, adverb,
adjective, conjuction, interjection, noun, preposition, verb, pronoun);
System.out.println(" ! Loading completed... ");
}
public void printWord(String keyword, String partOfSpeech, String Param,String Param2) {
System.out.println("|");
foundPOS = foundPOSIsDistinct = foundKeyWord = false;
boolean distinct =false;
boolean reverse = false;
dataStructure.forEach((word)->{
if(word.toString().toLowerCase().equals(keyword.toLowerCase())){
foundKeyWord =true;
}
});
if(foundKeyWord){
dataStructure.forEach((word)->{
if(word.toString().toLowerCase().equals(partOfSpeech.toLowerCase())){
if(word.isPOS()){
foundPOS =true;
}
}
});
if(!partOfSpeech.trim().equals("") && !foundPOS && !partOfSpeech.toLowerCase().equals("distinct") && !partOfSpeech.toLowerCase().equals("reverse") ){
System.out.println("
System.out.println("
System.out.println("
System.out.println("
System.out.println("
System.out.println("| |");
}
if(!Param.trim().equals("") && !Param.toLowerCase().equals("distinct") && !Param.toLowerCase().equals("reverse")){
System.out.println("
System.out.println("
System.out.println("
System.out.println("
System.out.println("| |");
}
if(!Param2.trim().equals("") && !Param2.toLowerCase().equals("reverse")){
System.out.println("
System.out.println("
System.out.println("
System.out.println("| |");
}
if(partOfSpeech.toLowerCase().equals("distinct") || Param.toLowerCase().equals("distinct")){
distinct =true;
}
if(partOfSpeech.toLowerCase().equals("reverse")|| Param.toLowerCase().equals("reverse") || Param2.toLowerCase().equals("reverse")){
reverse = true;
}
boolean finalDistinct = distinct;
boolean finalReverse = reverse;
dataStructure.forEach((word)->{
if(word.toString().toLowerCase().equals(keyword.toLowerCase())){
if(foundPOS) {
print(partOfSpeech, word, finalDistinct, finalReverse);
}
else{
printall(word, finalDistinct, finalReverse);
}
}
});
}
else{
System.out.println("
Dictionary.showHelp();
}
System.out.println("|");
}
public void print(String partOfSpeech, DictionaryWord word, boolean distinct,boolean reverse) {
ArrayList
if (partOfSpeech.toLowerCase().equals(DictionaryWord.PREPOSITION.toString().toLowerCase())) {
message.addAll(printPreposition(word, distinct));
} else if (partOfSpeech.toLowerCase().equals(DictionaryWord.CONJUNCTION.toString().toLowerCase())) {
message.addAll(printConjuction(word, distinct) );
} else if (partOfSpeech.toLowerCase().equals(DictionaryWord.NOUN.toString().toLowerCase())) {
message.addAll(printNoun(word, distinct));
} else if (partOfSpeech.toLowerCase().equals(DictionaryWord.INTERJECTION.toString().toLowerCase())) {
message.addAll(printInterjection(word, distinct));
} else if (partOfSpeech.toLowerCase().equals(DictionaryWord.VERB.toString().toLowerCase())) {
message.addAll(printVerb(word, distinct) );
} else if (partOfSpeech.toLowerCase().equals(DictionaryWord.ADJECTIVE.toString().toLowerCase())) {
message.addAll(printAdjective(word, distinct));
} else if (partOfSpeech.toLowerCase().equals(DictionaryWord.ADVERB.toString().toLowerCase())) {
message.addAll(printAdverb(word, distinct));
} else if (partOfSpeech.toLowerCase().equals(DictionaryWord.PRONOUN.toString().toLowerCase())) {
message.addAll(printPronoun(word, distinct));
}
if (message.size() > 0) {
if(reverse){
Collections.reverse(message);
}
message.forEach((msg)->{
System.out.println(msg);
});
} else {
System.out.println("
Dictionary.showHelp();
}
}
public ArrayList
ArrayList
if (!distinct) {
if (mWord.getNoun() != null) {
for (Object noun : mWord.getNoun()) {
message.add((mWord.toString().toUpperCase()) + " [noun] : " + noun );
}
}
} else {
if (mWord.getDistinctNoun() != null) {
for (Object noun : mWord.getDistinctNoun()) {
message.add((mWord.toString().toUpperCase()) + " [noun] : " + noun );
}
}
}
return message;
}
public ArrayList
ArrayList
if (!distinct) {
if (mWord.getAdjective() != null) {
for (Object adjective : mWord.getAdjective()) {
message.add((mWord.toString().toUpperCase()) + " [adjective] : " + adjective );
}
}
} else {
if (mWord.getDistinctAdjective() != null) {
for (Object adjective : mWord.getDistinctAdjective()) {
message.add((mWord.toString().toUpperCase()) + " [adjective] : " + adjective );
}
}
}
return message;
}
public ArrayList
ArrayList
if (!distinct) {
if (mWord.getAdverb() != null) {
for (Object adverb : mWord.getAdverb()) {
message.add((mWord.toString().toUpperCase()) + " [adverb] : " + adverb) ;
}
}
} else {
if (mWord.getDistinctAdverb() != null) {
for (Object adverb : mWord.getDistinctAdverb()) {
message.add((mWord.toString().toUpperCase()) + " [adverb] : " + adverb );
}
}
}
return message;
}
public ArrayList
ArrayList
if (!distinct) {
if (mWord.getConjuction() != null) {
for (Object conjuction : mWord.getConjuction()) {
message.add((mWord.toString().toUpperCase()) + " [conjuction] : " + conjuction);
}
}
} else {
if (mWord.getDistinctConjuction() != null) {
for (Object conjuction : mWord.getDistinctConjuction()) {
message.add((mWord.toString().toUpperCase()) + " [conjuction] : " + conjuction );
}
}
}
return message;
}
public ArrayList
ArrayList
if (!distinct) {
if (mWord.getInterjection() != null) {
for (Object interjection : mWord.getInterjection()) {
message.add((mWord.toString().toUpperCase()) + " [interjection] : " + interjection);
}
}
} else {
if (mWord.getDistinctInterjection() != null) {
for (Object interjection : mWord.getDistinctInterjection()) {
message.add((mWord.toString().toUpperCase()) + " [interjection] : " + interjection);
}
}
}
return message;
}
public ArrayList
ArrayList
if (!distinct) {
if (mWord.getPreposition() != null) {
for (Object preposition : mWord.getPreposition()) {
message.add((mWord.toString().toUpperCase()) + " [preposition] : " + preposition) ;
}
}
} else {
if (mWord.getDistinctPreposition() != null) {
for (Object preposition : mWord.getDistinctPreposition()) {
message.add((mWord.toString().toUpperCase()) + " [preposition] : " + preposition);
}
}
}
return message;
}
public ArrayList
ArrayList
if (!distinct) {
if (mWord.getPronoun() != null) {
for (Object pronoun : mWord.getPronoun()) {
message.add((mWord.toString().toUpperCase()) + " [pronoun] : " + pronoun);
}
}
} else {
if (mWord.getDistinctPronoun() != null) {
for (Object pronoun : mWord.getDistinctPronoun()) {
message.add((mWord.toString().toUpperCase()) + " [pronoun] : " + pronoun);
}
}
}
return message;
}
public ArrayList
ArrayList
if (!distinct) {
if (mWord.getVerb() != null) {
for (Object verb : mWord.getVerb()) {
message.add((mWord.toString().toUpperCase()) + " [verb] : " + verb );
}
}
} else {
if (mWord.getDistinctVerb() != null) {
for (Object verb : mWord.getDistinctVerb()) {
message.add((mWord.toString().toUpperCase()) + " [verb] : " + verb );
}
}
}
return message;
}
public void printall(DictionaryWord word, boolean distinct,boolean reverse) {
ArrayList
message.addAll(printAdjective(word, distinct));
message.addAll(printAdverb(word, distinct));
message.addAll(printConjuction(word, distinct) );
message.addAll(printInterjection(word, distinct) );
message.addAll(printNoun(word, distinct) );
message.addAll(printPreposition(word, distinct));
message.addAll(printPronoun(word, distinct)) ;
message.addAll(printVerb(word, distinct)) ;
if (message.size() !=0) {
if(reverse){
Collections.reverse(message);
}
message.forEach((msg)->{
System.out.println(msg);
});
} else {
System.out.println("
}
}
public void filterParams(String params[]) {
String keyword = "";
String pos = "";
String lastparam = "";
String lastParam1="";
boolean paramsIsValid = true;
switch (params.length) {
case 1:
keyword = params[0];
break;
case 2:
keyword = params[0];
pos = params[1];
break;
case 3:
keyword = params[0];
pos = params[1];
lastparam = params[2];
break;
case 4:
keyword = params[0];
pos = params[1];
lastparam = params[2];
lastParam1 = params[3];
break;
default:
paramsIsValid = false;
Dictionary.showHelp();
break;
}
if (paramsIsValid) {
printWord(keyword, pos, lastparam,lastParam1);
}
}
public static void showHelp(){
System.out.println("| PARAMETER HOW-TO, please enter:");
System.out.println("1. A search key -then 2. An optional part of speech -then");
System.out.println("3. An optional distinct' -then 4. An optional 'reverse'");
System.out.println("|");
}
public static void main(String[] args) {
Dictionary dictionary = new Dictionary();
dictionary.loadData();
System.out.println("-----DICTIONARY 340 JAVA----- ");
System.out.println("----- Keywords 19 ");
System.out.println("----- Definitions: 61 ");
String userinput ="";
int counter=0;
while (!(userinput.equals("!Q") || userinput.equals("!q"))) {
if(counter!=0 && (userinput.toLowerCase().equals("!help") || userinput.trim().equals(""))){
Dictionary.showHelp();
}
else if(!userinput.trim().equals("")) {
dictionary.filterParams(userinput.split(" "));
}
System.out.print("Search ["+ ++counter+"]: ");
Scanner mScanner = new Scanner(System.in);
userinput = mScanner.nextLine();
}
System.out.println(" -----THANK YOU-----");
}
}
Complete Sample Output:
![Please help to correct the output order for "distinct distinct" Search[9], "reverse](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/10/670f9e3f4270b_799670f9e3f207ae.jpg)




! Loading data. .. Loading completed. .. DICTIONARY 340 JAVA Keywords: 19 Definitions: 61 Search [1]: !help PARAMETER HOW-TO, please enter: 1. A search key -then 2. An optional part of speech -then 3. An optional 'distinct' -then 4. An optional ' reverse' Search [2] : PARAMETER HOW-TO, please enter: 1. A search key -then 2. An optional part of speech -then 3. An optional 'distinct' -then 4. An optional 'reverse' Search [3]: aRrow Arrow [noun] : Here is one arrow:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
