Map & HashMap

profileml1391402

In this assignment, you will implement a generic map interface in two ways:

  1. as a hash table
  2. with another map implementation from the activity (attached)

If you prefer, you may implement and test maps of Strings (sample code in the right column below).

You should use normal Java arrays for storage, not ArrayList or other classes from the Java Collections API.

Your submission should be a zip file that includes the following files:

  • IMap.java (below, no changes needed)
  • IMapTest.java, HashMapTest.java, and OtherMapTest.java (below, no changes needed)
    • use the first column for Generic version, second column for String version
  • HashMap.java (your hash table implementation) (Here is the main task)
  • OtherMap.java (your other implementation) (Here is the main task)

 

Sample Code for Generics

Sample Code for Strings (not Generic)

interface IMap<T> {
    boolean del   (int key);        // delete key & its value
    T       get   (int key);        // get value for key
    boolean put   (int key, T val); // put key,value in map
    boolean hasKey(int key);        // is key in map?
    boolean hasVal(T   val);        // is val in map?
    void    clear ();               // remove all keys & values
    String  dump  ();               // return String with contents
    int     size  ();               // return # of pairs
}
interface IMap {
    boolean del   (int key);        // delete key & its value
    String  get   (int key);        // get value for key
    boolean put   (int key, String val); // put key,value in map
    boolean hasKey(int key);        // is key in map?
    boolean hasVal(String val);        // is val in map?
    void    clear ();                // remove all keys & values
    String  dump  ();                // return String with contents
    int     size  ();                // return # of pairs
}
import static org.junit.Assert.*;
import org.junit.*;

public abstract class IMapTest {
    protected IMap<String> map;

    // each subclass should initialize map and then call super.before()
    public void before() {
        assertNotNull(this.map);
        this.map.put(1,"one");
    }

    @After public void after() {
        this.map = null;
    }

    @Test public void constructor() {
        assertNotNull(this.map);
    }
    
    @Test public void clear() {
        assertEquals(1, this.map.size());
        this.map.clear();
        assertEquals(0, this.map.size());
    }

    @Test public void del() {
        assertEquals(1, this.map.size());
        assertFalse (   this.map.del(0));
        assertTrue  (   this.map.del(1));
        assertFalse (   this.map.del(1));
        assertEquals(0, this.map.size());
    }
    
    @Test public void get() {
        assertNull  (       this.map.get(0));
        assertEquals("one", this.map.get(1));
    }

    @Test public void has() {
        assertTrue (this.map.hasKey(1));
        assertTrue (this.map.hasVal("one"));
        assertFalse(this.map.hasKey(3));
        assertFalse(this.map.hasVal("three"));
        assertTrue (this.map.put(3,"three"));
        assertTrue (this.map.hasKey(3));
        assertTrue (this.map.hasVal("three"));
    }

    @Test public void put() {
        assertEquals(1, this.map.size());
        assertFalse (   this.map.put(1,"one"));
        assertTrue  (   this.map.put(3,"three"));
        assertFalse (   this.map.put(3,"three"));
        assertEquals(2, this.map.size());
    }
}
import static org.junit.Assert.*;
import org.junit.*;

public abstract class IMapTest {
    protected IMap map;

    // each subclass should initialize map and then call super.before()
    public void before() {
        assertNotNull(this.map);
        this.map.put(1,"one");
    }

    @After public void after() {
        this.map = null;
    }

    @Test public void constructor() {
        assertNotNull(this.map);
    }
    
    @Test public void clear() {
        assertEquals(1, this.map.size());
        this.map.clear();
        assertEquals(0, this.map.size());
    }

    @Test public void del() {
        assertEquals(1, this.map.size());
        assertFalse (   this.map.del(0));
        assertTrue  (   this.map.del(1));
        assertFalse (   this.map.del(1));
        assertEquals(0, this.map.size());
    }
    
    @Test public void get() {
        assertNull  (       this.map.get(0));
        assertEquals("one", this.map.get(1));
    }

    @Test public void has() {
        assertTrue (this.map.hasKey(1));
        assertTrue (this.map.hasVal("one"));
        assertFalse(this.map.hasKey(3));
        assertFalse(this.map.hasVal("three"));
        assertTrue (this.map.put(3,"three"));
        assertTrue (this.map.hasKey(3));
        assertTrue (this.map.hasVal("three"));
    }

    @Test public void put() {
        assertEquals(1, this.map.size());
        assertFalse (   this.map.put(1,"one"));
        assertTrue  (   this.map.put(3,"three"));
        assertFalse (   this.map.put(3,"three"));
        assertEquals(2, this.map.size());
    }
}
 
import static org.junit.Assert.*;
import org.junit.*;
public class HashMapTest extends IMapTest {
    @Before public void before() {
        this.map = new HashMap<String>(10);
        super.before();
    }
}
 
import static org.junit.Assert.*;
import org.junit.*;
public class HashMapTest extends IMapTest {
    @Before public void before() {
        this.map = new HashMap(10);
        super.before();
    }
}
 
import static org.junit.Assert.*;
import org.junit.*;
public class OtherMapTest extends IMapTest {
    @Before public void before() {
        this.map = new OtherMap<String>(10);
        super.before();
    }
}
 
import static org.junit.Assert.*;
import org.junit.*;
public class OtherMapTest extends IMapTest {
    @Before public void before() {
        this.map = new OtherMap(10);
        super.before();
    }
}
  • 10 years ago
  • 8
Answer(0)
Bids(0)