Skip to content

Commit

Permalink
added lecture 13 code
Browse files Browse the repository at this point in the history
  • Loading branch information
joshHug committed Feb 15, 2017
1 parent 4e039a3 commit 66a5e81
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
50 changes: 50 additions & 0 deletions syntax1/DIY/Map61B/ArrayMap.java
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"));
}
}
20 changes: 20 additions & 0 deletions syntax1/DIY/Map61B/Map61B.java
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();
}
39 changes: 39 additions & 0 deletions syntax1/DIY/Map61B/MapHelper.java
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);*/
}
}

0 comments on commit 66a5e81

Please sign in to comment.