Question

Write the following program in Java:

You are to write a Java program that reads an input file containing a list of words and their synonyms. You will repeatedly ask the user if they want to look up the synonyms of a word, add a new synonym for a word, or quit. Here is a sample run of the program (note that the program is expecting the input file, thesaurus.txt to be in the working directory for the code.): Enter (l)ookup, (a)dd entry, or (aDuit: l Enter word: graphic Result: explicit, descriptive, pictorial, lifelike, vivid Enter (l)ookup, (a)dd entry, or (qDuit: A Enter word: graphic Enter synonym: explicit Error: cannot add entry Enter (l)ookup, (a)dd entry, or (q)uit:l Enter word: hilarious Result: uproarious Enter (l)ookup, (a)dd entry, or(q)uit: a Enter word: Hilarious Enter synonym: very funny Enter (l)ookup, (a)dd entry, or (q)uit:l Enter word: hilarious Result: uproarious, very funny Enter (l)ookup, (a)dd entry, or (q)uit:l Enter word: miniscule Result: miniscule not found Enter (l)ookup, (a)dd entry, or(q)uit: a Enter word: miniscule Enter synonym: very small Enter (l)ookup, (a)dd entry, or(q)uit: IL Enter word: miniscule Result: very small Enter (l)ookup, (a)dd entry, or(q)uit: qmedia%2F402%2F402c98b8-ca8a-4857-83ca-c0media%2F484%2F4846830f-a800-42ea-af9c-d0media%2F7f6%2F7f6bd650-405d-430e-8ac3-a5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Entry.java:

package thesaurus;

class Entry

{

private static final int MAX_SYNONYMS = 10;

private String word;

private String[] synonyms;

private int synCount;

Entry(String word)

{

this.word = word;

this.synonyms = new String[MAX_SYNONYMS];

synCount = 0;

}

public String getWord()

{

return this.word;

}

public boolean addSynonym(String syn)

{

if(synCount < synonyms.length)

{

for(int i=0; i<synCount; i++)

{

if(isSynonym(syn)) return false;

}

synonyms[synCount++] = syn;

return true;

}

return false;

}

public String getSynonyms()

{

StringBuilder sb = new StringBuilder();

for(int i=0; i<synCount-1; i++)

{

sb.append(synonyms[i] + ", ");

}

sb.append(synonyms[synCount-1]);

return sb.toString();

}

public boolean isSynonym(String syn)

{

for(int i=0; i<synCount; i++)

{

if(synonyms[i].equalsIgnoreCase(syn)) return true;

}

return false;

}

}

Thesaurus.java:

package thesaurus;

class Thesaurus

{

private static final int MAX_ENTRIES = 7000;

Entry[] entries;

int entryCount;

Thesaurus()

{

entries = new Entry[MAX_ENTRIES];

entryCount = 0;

}

public Entry getEntry(String word)

{

for(int i=0; i<entryCount; i++)

{

if(entries[i].getWord().equalsIgnoreCase(word))

return entries[i];

}

return null;

}

public boolean addEntry(String word, String syn)

{

Entry entry = getEntry(word);

if(entry == null && entryCount < entries.length)

{

entry = new Entry(word);

entry.addSynonym(syn);

entries[entryCount++] = entry;

return true;

}

else if(entry != null)

{

return entry.addSynonym(syn);

}

else return false;

}

public String lookup(String word)

{

Entry entry = getEntry(word);

if(entry != null)

{

return entry.getSynonyms();

}

return word + " not found";

}

}

Proj6.java:

package thesaurus;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

class Proj6

{

public static void main(String[] args) throws FileNotFoundException

{

Thesaurus thesaurus = new Thesaurus();

Scanner inputFile = new Scanner(new File("thesaurus.txt"));

while(inputFile.hasNextLine())

{

// take the 1st word in the line as word.

String word = inputFile.next();

// read rest of the string of the line.

// then split it based on whitespace delimiter.

String[] syns = inputFile.nextLine().split(" ");

// for each of the synonym

for(String syn : syns)

{

// add the word and synonym to the thesaurus.

if(!thesaurus.addEntry(word, syn))

{

System.out.println("Error adding synonym " + syn + " for the word " + word);

}

}

}

inputFile.close();

Scanner in = new Scanner(System.in);

while(true)

{

System.out.print("Enter (l)ookup, (a)dd entry, or (quit): ");

String userSelection = in.nextLine().toLowerCase();

switch(userSelection)

{

case "l":

System.out.print("Enter word: ");

Entry e = thesaurus.getEntry(in.nextLine());

System.out.println("Result: " + e.getSynonyms());

break;

case "a":

System.out.print("Enter word:");

String word = in.nextLine();

System.out.print("Enter synonym: ");

String syn = in.nextLine();

if(!thesaurus.addEntry(word, syn))

System.out.println("Error: Can't add entry");

break;

case "q": in.close(); return;

}

}

}

}

SAMPLE OUTPUT:

Problems @Javadoc [B Declaration Console X P <terminated> Proj6 [Java Application] /usr/lib/jvm/java-11-ope Error adding synonym for the word word Enter (1)ookup, (a)dd entry, or (quit): ї Enter word: wordl Result:, synl, syn2, syn3 Enter (ookup, (a)dd entry, or (quit): a Enter word:wordl Enter synonym: syn99 Enter (1)ookup, (a)dd entry, or (quit): ї Enter word: wordl Result: , synl, syn2, syn3, syn99 Enter (ookup, (a)dd entry, or (quit):q

Add a comment
Know the answer?
Add Answer to:
Write the following program in Java: You are to write a Java program that reads an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This java assignment will give you practice with classes, methods, and arrays. Part 1: Player Class...

    This java assignment will give you practice with classes, methods, and arrays. Part 1: Player Class Write a class named Player that stores a player’s name and the player’s high score. A player is described by:  player’s name  player’s high score In your class, include:  instance data variables  two constructors  getters and setters  include appropriate value checks when applicable  a toString method Part 2: PlayersList Class Write a class that manages a list...

  • CÖ) Could you please help me to solve this problem?(ONLY USING C++ PROGRAMMING LANGUAGE PLEASE) Write...

    CÖ) Could you please help me to solve this problem?(ONLY USING C++ PROGRAMMING LANGUAGE PLEASE) Write a program for the patient information system in a policlinic. The patients are examined according to the triage char assigned to them. Triage codes are as follows: - ‘h’ : high priority - ‘m’ : medium priority - ‘l’ : low priority The patients having ‘h’ triage char are examined first then the patients having ‘m’ comes and finally patients with ‘l’ triage code...

  • Write a java program for the following: Your program reads an infix expression represented by a...

    Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...

  • Could you guys write an efficient java program to implement BST. Your java program should read...

    Could you guys write an efficient java program to implement BST. Your java program should read words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in xxxxx5.java file (5th java project), where xxxxx is the first 5 characters of your last name. To compile: javac xxxxx5.java To execute: java xxxxx5 < any data file name Your main method should...

  • Today you are to write a Java program that will prompt for and read 2 words...

    Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in...

  • Write a Java Program that reads the following: Ask the user for a balance. From the...

    Write a Java Program that reads the following: Ask the user for a balance. From the accounts.dat file, display all the accounts that have a balance of at least what the user entered. accounts.dat is a binary file. Each entry contains a credit card number (long), a balance (double), and a cash back flag (boolean). Question 1 10 points Ask the user for a balance. From the accounts.dat file.display all the accounts that have a balance of at least what...

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

  • Write a program in Java according to the following specifications: The program reads a text file...

    Write a program in Java according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "print" - prints the student records (first name, last name, grade). "sortfirst" - sorts the student records by first name. "sortlast" - sorts the student records by last name. "sortgrade" - sorts the...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT