You will replace the back-end for Project #1 to permit persistence of the changes made to the Rashty-English dictionary as they happen. The GUI and the functionality of the program should remain the same.
You will first develop a utility to initially create a Random Access File that holds our English/Rashty word pairs. You will then develop a class named externalDictionary which will represent the dictionary externally. From the perspective of your application, you will no longer use the Map object that help the dictionary; you will now construct an externalDictionary object to access and manipulate it in response to user requests.
Use the following skeleton in building your externalDictionary class. The class Vector is in java.util and is used here to keep track of Rashty words in the dictionary file. For the most part, Vectors act like arrays, but can be constructed so that they can be extendible. Each word stored in this vector will correspond to a Rashty/English entry in the RAF created in the previous section. We use the vector to avoid going to the file until we know exactly which record needs to be read or written to.
import java.io.*; import java.util.*; public class externalDictionary { static final int RECSIZE=50; //size of each record in bytes. fixedLengthRecordRW f; //FLR random access file. int current_=-1; //The iterator for our external file. Vector word_; //A vector holding each Rashty word, the index of //each word corresponds to where the //English/Rashty pair is in the file. public externalDictionary () { /* open the fixedLengthRecordRW with English/Rashty words and build the vector. If there are records in the file, set current_ to zero; otherwise, to -1. */ } public String find (String r) { /* Given a Rashty word, find it in the vector, read the appropriate record from the file and return its corresponding English word. return null, if the Rashty word is not found */ } public String add (String r, String e) throws invalidEnglish, invalidRashty{ /* You must first validate the two words. Exception classes will be needed. Given a valid English and Rashty pair build a record using the record format you established when writing your utility. Look for the Rashty word in the vector; if exists, replace the corresponding file entry with the new version; if it doesn't, insert the new record in the appropriate location of the file as well as adding the Rashty word to the vector. The words in the Vector must be kept in alpha order. The records in the file must also be kept in the order of the Rashty words. Of course, this means that, most of the time, you shift words in the vector and the records in the file to make room for new entries. This method, returns the old English translation if one already existed, otherwise, it returns null. */ } public void remove (String r) { /* Given a Rashty word, look for the word in the vector. if exists, remove it from both the vector and the file; in the case of the file, move all record after it back by one and truncate the file. Do nothing, if the word is not in vector. */ } public void First () { /* Set current to Zero so that it points to the first record in the file. Do nothing if file is empty. */ } public void Advance () { /* Advance current so that it points to the next record in the file. Do nothing if already on the last record. */ } public int current () { /* return current. */ } public String Retrieve () { /*Read the record addressed by current; convert it to a String and return it. If current is not valid (i.e. 0..Last()), return null */ } public int Last () { /*return the position of the last record in the file. */ } }