CSC103, Spring 2004, Project 5: Hash Table with Open Addressing and quadratic probing Due Date: 06/02, Thursday for section 06; 06/03 Wednesday for sections 01 and 03. Due Time: beginning of your lecture hour. Instructor: Dr. Hasmik Gharibyan IMPORTANT: DON’T CHANGE any names, menu choices, sequence of steps in assignment. Failure to do so will end with penalty. I. Define an interface Hashable with one abstract method hash. II. Design and implement a class HashTable to represent a collection of elements arranged as a hash table with open addressing and quadratic probing. Few agreements before we start: 1. Duplicates of elements are not allowed (a duplicate will not be saved). 2. The table gets rehashed as soon as it gets half full. 3. An inactive entry of a table represents a deleted element of the collection. Thus, elements of the collection are the ones stored in active entries of the table. 4. The cell of the hash table containing an inactive entry is considered occupied. 5. Objects of HashTable class should be serializable. HashTable class has the following members: 1.ONLY TWO data members: (i) an array of hash entries to hold the table, and (ii) an int variable for holding the number of occupied cells in the table (active or inactive). 2. nested private class HashEntry, objects of which form the table (see lecture handout). Note: Since HashTable objects are serializable, so should be HashEntry objects. 3. nested private iterator class Iter, objects of which are iterators for scanning through the elements of the collection. Your iterator should rest only on elements of the collection, i.e. it should ignore inactive entries of the hash table, as well as empty cells. 4. one constructor – to create an empty hash table. It has one parameter, an int value, used to define the size of the table, which is always a prime number (the size of the table is the first prime number greater or equal to the value of the parameter). 5. the following public methods (execution of these methods is as defined in the handout). public void insert (Hashable item), to add an entry containing item to the table. Nothing is done if the item is already in the table and is active. The table gets rehashed, if the insertion causes the table to get half full (the size of the new table should be prime). public void delete (Hashable item), to delete the item from the table (lazy deletion). Nothing is done if the item is not in the table, or is already deleted. No special treatment of empty list (equivalent to “item not found”). public Hashable find (Hashable item), to check if item is in collection or not (or more precisely, to check if an object with the same key as item exists in the collection). Returns the object of collection if found, and null otherwise. No special treatment of empty list (equivalent to “not found”). public int elementCount(), to return the number of elements present in the collection. (i.e. the number of active entries in the table) 1 public boolean isEmpty(), to check if the collection is empty (checks if there are any active cells or not). Returns true/false. public void makeEmpty(), to make the table empty (make all cells unoccupied). public void printTable(), to print the table content (the whole content, including unoccupied cells, as well as cells with inactive entries). The content of each cell of the array (table) is output on one line. Each output line starts with the index of the table cell and is followed either by the word “empty” (for empty cells) or by the content of the cell (i.e. the element contained in the entry and its status “active”/”inactive”). Have your printout in the following format: [0]: xxxx, active [1]: empty [2]: yyyy, inactive ……………………. public Iterator iterator (), to create a new Iter object for the collection. public void serialize(String filename), to save this object into the given file. public static HashTable deserialize(String filename), to retrieve an object from the file. III. Implement a class Student. Objects of this class will be used in the driver to form a collection, which will be arranged in a hash table; therefore this class should implement the Hashable interface. And since the hash table may be serialized, this class should implement the Serializable interface. An object of Student class is to represent one student’s information record, which contains the following pieces of information about a student: - student id, //Agreement: this is an integer number larger than 0 and less than 10,000. - name, //the first and last name of a student separated by a comma: e.g. Smith,William //Agreement: there is NO space in the string representing the two pieces of name. - gpa. //this is a float value The Student class has the following members: 1) three private instance variables to hold a student record. 2) one constructor (with 3 parameters of appropriate types), to initialize instance variables of a newly created object. 3) the following methods: public boolean equals( Object other), to return true if the id of this object is the same as the id of the parameter object and false otherwise. public String toString(), to return a string containing the name, id and gpa of a student in the following format: { id#1054: Smith,William, GPA 3.15} Note: the non-bold italic font is used for values contained in the Student object. public int hash(int tablesize), to return the hash value of this object for the given table size. The student id is used as a key in hashing, and since it is an integer value, a simple modulus hashing is performed. IV. Prepare an input file for your program. This file is a data file and contains the collection that needs to be given to the program. The first line contains one integer - the number of student records in the collection. Starting with the second line the collection itself is stored: one student record per line, on each line three values separated by spaces: (i) student id, (ii) student’s name (as agreed, no blanks in student’s name), and (iii) student’s GPA. There should be at least as many student records in the file as indicated by the integer on the first line. 2 V. Implement a driver HTDriver for testing HashTable class’s functionality. In this driver you need to 1) Prompt the user to give the name of the input file where the data are stored. DO NOT hard code the name of the file in your program, it must be obtained from the user. 2) Read the name of the input file and define the streams for File Input. 3) Read the integer given on the first line of the input file (let’s refer to this value as N), and create a HashTable object of tablesize twice as large as the entered value (i.e. give 2*N as parameter when creating the HashTable object). 4) Insert exactly N Student objects into the hash table. Objects are created by reading values from input file (each line of the file, starting with the second, contains one student’s info). Attention: IGNORE a student record, if it contains invalid data such as - missing or additional value, - invalid key (not an integer, or an integer not in range [1, 9999]), - invalid GPA (not a float value, or a float value not in range [1,4]), 5) For as long as the user wants (until he/she chooses to quit by entering ‘q’ as menu option). 1. Provide the user with a menu of operations for HashTable class, and request a choice. Important: DO NOT CHANGE the letters provided in this menu – the grading program will fail on your code if you use other letters. Your menu should look like this: Choose one of the following operations by entering provided letter: a – add the element d – delete the element f – find and retrieve the element n – get the number of elements in the collection e – check if the collection is empty k – make the hash table empty p – print the content of the hash table o – output the elements of the collection s – serialize the object into a file r – retrieve a saved object from the file q – Quit the program Enter your choice: a) Enter user’s choice (should be one letter on one line), b) Analyze user’s choice and arrange the chosen operation’s performance: - prompt for and input parameter values if necessary. - make necessary checks on validity of parameter values, if any. - arrange the execution of the request. - Give feedback to the user. 2. Attention: 1) DO NOT input parameter values on the same line as the menu choice: 2) Always prompt the user to enter the parameter values. IMPORTANT CLARIFICATIONS!!! 1. p option is for printing the table using the printTable method of HashTable class. 2. o option is for printing elements of the collection using an iterator for HashTable object. Each student record is output on one separate line. 3. f option is for retrieving the Student object by its key: you need to (i) prompt the user to enter one integer only for a key to be searched by, (ii) create a dummy Student 3 object with that key and give it to find method as a parameter. find method will return the object with the matching key, which you need to output for the user to see. Attention: output an error message and ignore the request, if the entered key value is not valid (i.e. is not an integer, or is an integer not in range [1, 9999]). DO NOT ask the user to reenter the value. 4. d option is for deleting a Student object by its key: you need to (i) prompt the user to enter one integer only for a key to be searched by, (ii) create a dummy Student object with that key and give it to delete method as a parameter. delete method will make the object with the matching key inactive. Attention: output an error message and ignore the request, if the entered key value is not valid (i.e. is not an integer, or is an integer not in range [1, 9999]). DO NOT ask the user to reenter the value. 5. When adding a new student to the collection, prompt the user to enter the three values representing the student record. Values must be given on the same line and be separated by spaces. Attention: output an error message and ignore the request, if inputted values contain invalid data such as - missing or additional value, - invalid key (not an integer, or an integer not in range [1, 9999]), - invalid GPA (not a float value, or a float value not in range [1,4]), In case of invalid data DO NOT ask the user to reenter the value. 6. When serializing/deserializing a HashTable object, prompt the user for the file name and input it from the keyboard. DO NOT hard code the name of the file in your program. 6) Finish the program by outputting a Farewell message. TEST YOUR PROGRAM THOROUGHLY. Make sure it works correctly for all hash table operations and services. Important: Your code should be self documented: have a header for each file, include pre- and postconditions for every method, insert comments for variable definitions, describe steps that are not obvious. You’ll loose points for poor documentation of your program. Submitting your work: 1. Turn in all your files ( all .java and .class files, as well as the input file containing the collecttion) via UNIX “turnin” procedure by the beginning of your lecture hour on the due date. The name of this assignment is Program5. 2. Submit the following documents at the beginning of the lecture hour on the due date. - A cover sheet with your name, course, section and assingment number; instructor’s name. - The text of your program (the source code of your classes: HashTable, Hashable, Student, HTDriver) - the printout of your input file. No output is necessary. 4