forked from Berkeley-CS61B/lectureCode-sp17
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package Map61B; | ||
|
||
import org.junit.Assert.*; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* An array based implementation of the Map61B class. | ||
*/ | ||
public class ArrayMap<K, V> implements Map61B<K, V> { | ||
public ArrayMap() { | ||
} | ||
|
||
/** Returns the index of the given key if it exists, | ||
* -1 otherwise. */ | ||
private int keyIndex(K key) { | ||
return 0; | ||
} | ||
|
||
|
||
public boolean containsKey(K key) { | ||
return false; | ||
} | ||
|
||
public void put(K key, V value) { | ||
} | ||
|
||
public V get(K key) { | ||
return false | ||
} | ||
|
||
public int size() { | ||
return 0; | ||
} | ||
|
||
public List<K> keys() { | ||
return null; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Map61B<String, Integer> m = new ArrayMap<String, Integer>(); | ||
m.put("horse", 3); | ||
m.put("fish", 9); | ||
m.put("house", 10); | ||
int expected = 9; | ||
assertEquals((Integer) expected, m.get("fish")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package Map61B; | ||
import java.util.List; | ||
|
||
public interface Map61B<K, V> { | ||
/* Returns true if this map contains a mapping for the specified key. */ | ||
boolean containsKey(K key); | ||
|
||
/* Returns the value to which the specified key is mapped. No defined | ||
* behavior if the key doesn't exist (ok to crash). */ | ||
V get(K key); | ||
|
||
/* Returns the number of key-value mappings in this map. */ | ||
int size(); | ||
|
||
/* Associates the specified value with the specified key in this map. */ | ||
void put(K key, V value); | ||
|
||
/* Returns a list of the keys in this map. */ | ||
List<K> keys(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package Map61B; | ||
|
||
import static org.junit.Assert.*; | ||
import org.junit.Test; | ||
import java.util.List; | ||
|
||
/** | ||
* Class to demonstrate how generic methods work in Java. | ||
*/ | ||
public class MapHelper { | ||
/* Write the following three methods: | ||
/* get(Key) : Return item in map if it exists. */ | ||
/* maxKey() : Returns max of all keys. Works only if x and y have comparable data. */ | ||
/* allBark(): Makes all keys bark, but only works for Dogs. */ | ||
|
||
@Test | ||
public void testGet() { | ||
Map61B<String, Integer> m = new ArrayMap<String, Integer>(); | ||
m.put("horse", 3); | ||
m.put("fish", 9); | ||
m.put("house", 10); | ||
|
||
/*Integer actual = MapHelper.get(m, "fish"); | ||
Integer expected = 9; | ||
assertEquals(expected, actual);*/ | ||
} | ||
|
||
@Test | ||
public void testMaxKey() { | ||
Map61B<String, Integer> m = new ArrayMap<String, Integer>(); | ||
m.put("horse", 3); | ||
m.put("fish", 9); | ||
m.put("house", 10); | ||
|
||
/*String actual = MapHelper.maxKey(m); | ||
String expected = "house"; | ||
assertEquals(expected, actual);*/ | ||
} | ||
} |