Write a French/English dictionary lookup program. Read a list of pairs of Englis
ID: 3864872 • Letter: W
Question
Write a French/English dictionary lookup program. Read a list of pairs of English and French words from a file specified by the user. English/French words should be exact matches (don't try to find partial matches).
Use the supplied EnglishFrenchDictionary.java class as your main class. Fill in the missing code in the DictionaryTable.java class to read the input file and perform the searches.
Add code to the DictionaryTable read() method to:
read pairs of lines (English word is on the first line, and the corresponding French word is on the second line), and for each pair of lines:
create an Item object for the English word and French word to store in the byEnglish array list
create an Item object for the French word and corresponding English word to store in the byFrench array list.
After reading all the words, sort the byEnglish and byFrench array lists so we can use binary search on them.
Add code to the DictionaryTable’s findFrench() method to:
Create an Item object with the specified English word and null for the French word (because we don’t know it yet)
Use the Collections.binarySearch() method to search for the matching Item in the byEnglish array list
If a matching Item is found, return the item’s value which is the corresponding French word (hint: use getValue()).
Add similar code to the DictionaryTable’s findEndlish() method to:
Create an Item object with the specified French word and null for the English word (because we don’t know it yet)
Use the Collections.binarySearch() method to search for the matching Item in the byFrench array list
If a matching Item is found, return the item’s value which is the corresponding English word (hint: use getValue()).
Two files have been supplied for testing your program: shortdictionary.txt and longdictionary.txt. found here: https://drive.google.com/drive/folders/0B41Z5suN4j28Um5xTEVSeGN0V0U?usp=sharing shortdictionary.txt has just a few words in it for quick testing, and longdictionary.txt has over 500 entries for more extensive testing.
The files have the format of an English word on one line, followed by the French word on the next line:
eight
huit
Amazon
amazone
april
avril
Arctic
arctique
British
britannique
. . .
A sample run of the program:
Enter the name of the dictionary file:
shortdictionary.txt
Lookup by E)nglish word, F)rench word, Q)uit?
F
Enter French word:
huit
English word: eight
Lookup by E)nglish word, F)rench word, Q)uit?
E
Enter English word:
eight
French word: huit
Lookup by E)nglish word, F)rench word, Q)uit?
Explanation / Answer
Please find the code and sample output:
//DictionaryTable.java
package Homework6;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
A table for lookups and reverse lookups
*/
public class DictionaryTable
{
private ArrayList<Item> byEnglish;
private ArrayList<Item> byFrench;
/**
Constructs a DictionaryTable object.
*/
public DictionaryTable()
{
byEnglish = new ArrayList<Item>();
byFrench = new ArrayList<Item>();
}
/**
Reads English and French word pairs from the Scanner
and adds them to the byEnglish and byFrench array lists.
@param in -- Scanner for reading the input
*/
public void read(Scanner in)
{
// TODO:
// Read pairs of lines - English word on one line and French on the next - until
// no more lines are in the input file.
// Create new Item object from the English - French word and add to byEnglish list.
// Create new Item object from the French - English word and add to byFrench list.
// When finished, be sure to sort the byEnglish and byFrench lists!
while(in.hasNext()){
String english = in.nextLine().trim();
String french = in.nextLine().trim();
Item englishItem = new Item(english, french);
byEnglish.add(englishItem);
Item frenchItem = new Item(french, english);
byFrench.add(frenchItem);
Collections.sort(byEnglish);
Collections.sort(byFrench);
}
}
/**
Looks up an item in the table.
@param english the key to find
@return the value with the given key, or "Not Found" if no
such item was found.
*/
public String findFrench(String english)
{
// TODO: Search the byEnglish array list for an Item with a matching english word,
// and returns its value.
Item key = new Item(english, null);
String value = byEnglish.get(Collections.binarySearch(byEnglish, key)).getValue();
// This is the default result:
return value!=null ? value:"Not Found";
}
/**
Looks up an item in the table.
@param french the value to find
@return the key with the given value, or "Not Found" if no
such item was found.
*/
public String findEnglish(String french)
{
// TODO: Search the byFrench array list for an Item with a matching french word,
// and returns its value.
Item key = new Item(french, null);
int index = Collections.binarySearch(byFrench, key);
String value = byFrench.get(Collections.binarySearch(byFrench, key)).getValue();
// This is the default result:
return value!=null ? value:"Not Found";
}
}
//EnglishFrenchDictionary.java
package Homework6;
import java.io.IOException;
import java.io.FileReader;
import java.util.Scanner;
/* The input file has the format of English word on one line
* followed by French word on the next line. The pairs continue
* until the end of the file.
*/
public class EnglishFrenchDictionary
{
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the dictionary file: ");
String fileName = in.nextLine();
DictionaryTable table = new DictionaryTable();
FileReader reader = new FileReader(fileName);
table.read(new Scanner(reader));
boolean more = true;
while (more)
{
System.out.println("Lookup by E)nglish word, F)rench word, Q)uit?");
String cmd = in.nextLine();
if (cmd.equalsIgnoreCase("Q"))
more = false;
else if (cmd.equalsIgnoreCase("E"))
{
System.out.println("Enter English word:");
String n = in.nextLine();
System.out.println("French word: " + table.findFrench(n));
}
else if (cmd.equalsIgnoreCase("F"))
{
System.out.println("Enter French word:");
String n = in.nextLine();
System.out.println("English word: " + table.findEnglish(n));
}
}
}
}
// Item.java
package Homework6;
/**
An item with a key and a value.
*/
public class Item implements Comparable<Item>
{
private String key;
private String value;
/**
Constructs an Item object.
@param k the key string
@param v the value of the item
*/
public Item(String k, String v)
{
key = k;
value = v;
}
/**
Gets the key.
@return the key
*/
public String getKey()
{
return key;
}
/**
Gets the value.
@return the value
*/
public String getValue()
{
return value;
}
public int compareTo(Item otherObject)
{
Item other = (Item) otherObject;
return key.compareTo(other.key);
}
}
// Output ----
Enter the name of the dictionary file:
shortdictionary.txt
Lookup by E)nglish word, F)rench word, Q)uit?
F
Enter French word:
huit
English word: eight
Lookup by E)nglish word, F)rench word, Q)uit?
E
Enter English word:
eight
French word: huit
Lookup by E)nglish word, F)rench word, Q)uit?
Q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.