CS 310
Program 3

Due: Thursday April 30, 2009.

The program

You will create a class MyHashMap that can map keys to values. Class MyHashMap will use the QuadraticProbingHashTable class in the text.

You will also write a simple application that will create a hash map of the mountains data used in assignment 2. The hash map will map each country to a priority queue of the mountains in that country.

Class QuadraticProbingHashTable

File QuadraticProbingHashTable.java from Chapter 5 of the text has been posted.

You are asked to make the following changes/extensions to class QuadraticProbingHashTable :

1. Implement method "public boolean isEmpty()", which returns true when there are no elements in the hash table, and false otherwise. Note: variable currentSize in class QuadraticProbingHashTable does *not* count the number of elements in the hash table.

2. Implement method "public AnyType find(AnyType x)", which returns a reference to element x if x is stored in the hash table, and returns null otherwise.

3. Modify method "public void insert(AnyType x)" so that if x is already in the hash table, the old value of x is overwritten with the new value.

4. Modify method "private int findPos(AnyType x)" so that new items are inserted into the first inactive cell on the search path, making it possible to reclaim deleted cells. Do this by having findpos() maintain, with an additional variable, the location of the first inactive cell it encounters.

5. Implement method "public Object[] toArray()", which returns an array of the elements stored in the hash table. If there are 3 active elements stored in the table, the length of the returned array should be 3.

Do not change the order of the methods in class QuadraticProbingHashTable. All the methods you need to write/modify are at the end of the class.

Test your work by executing the main() method in QuadraticProbingHashTable. You will be asked to submit the output from main().

Class MyHashMap

File MyHashMap.java has been posted.

Complete the implementation of class MyHashMap . The implementation will store a hash table of < Key,Value > pairs.

In the mountain data application described below, keys are country names and values are (Java) Priority Queues. A priority queue stores the names and altitude data for the mountains in a particular country. Priority is based on altitude - highest first.

public class MyHashMap {
	public MyHashMap() {
		items = new QuadraticProbingHashTable< Entry< KeyType,ValueType> >(); 
	} 

	public void put( KeyType key, ValueType val ) { } 

	public ValueType get( KeyType key ) { } 

	public boolean isEmpty() { }

	public void makeEmpty() { }
	
	public Set entryList() { }

	private QuadraticProbingHashTable< Entry< KeyType,ValueType> > items; 

	public static class Entry {

		private KeyType key;
		private ValueType val;
		
		Entry( KeyType k, ValueType v ) {
			key=k;
			val=v;
		}

		public int hashCode() {
			return key.hashCode();
		}

		public boolean equals( Object rhs ) {
			return rhs instanceof Entry && 
			key.equals( ((Entry)rhs).key ); 
		} 
		
		public KeyType getKey() {return key;}
		public ValueType getValue() {return val;}
	}
}

Test your work by executing the main() method in MyHashMap. You will be asked to submit the output from main().

The application

Your program will open the file containing the mountains data, and create a hash map as an instance of MyHashMap. Keys are country names stored as Java Strings, and values are (Java) priority queues. The PriorityQueue will contain one or more MountainRecord objects representing the mountains in a particular country. Each MountainRecord stores the name and altitude of a single mountain. The name value is stored as a Java Strings and the altitude value is stored as a Java Integer.

The program will then go into a loop displaying a menu offering the choices:

and accept the user's choice.

If the user chooses "insert a record" the program will prompt for a new mountain record. The format of the record entered by the user is the same as the format used in the mountains data file, i.e., three fields delimited by '#' characters.

If the country key already appears in the hash map, then the new mountain record is added to the Priority Queue value for the key. Otherwise a new pair is created and added to the hash map, along with the new mountain record.

If the user chooses "display highest mountains" the program will prompt for a country name (the key) and will display the three mountains with the highest altitude for that country, in the format:

Highest Mountains in United States: McKinley (Denali), Mount, altitude: 20320 Foraker, Mount, altitude: 17400 Bona / Churchill, Mount, altitude: 16421
If no key is found a "no mountains" message is displayed:
Highest Mountains in Nowhere: no mountains
The program will run until the user chooses "quit".

Using the Java Collections Library


Here are some links to some Java classes that are useful for this assignment:

Scanner

Console

StringTokenizer

LinkedList

PriorityQueue

Class PriorityQueues uses ascending order by default. You can switch to descending order by writing a class that implements the Comparator interface. This is discussed in the text in Section 1.6. Class PriorityQueue has a constructor that accepts a comparator object. Note: if a.compareTo(b) is ascending order, then b.compareTo(a) is descending order.

Submitting your assignment

You will hand in (all in hardcopy):

- your source code,

- the output generated by executing method main of class QuadraticProbingHashTable,

- the output generated by executing method main of class MyHashMap,

- a sample terminal session of your application, and

- a sheet stating which parts of the assignment (if any) are incomplete or known to be incorrect.

For the terminal session you will use the mountains data file from assignment 2. A list of commands to use for your terminal session will be posted.