ソースを参照

Added the third assignment.

Zach Marcin 7 年 前
コミット
db23ce6db9
共有4 個のファイルを変更した95 個の追加1 個の削除を含む
  1. 8
    1
      README.md
  2. 20
    0
      src/main/java/Table/Entry.java
  3. 17
    0
      src/main/java/Table/Table.java
  4. 50
    0
      src/test/java/Table/TableTest.java

+ 8
- 1
README.md ファイルの表示

@@ -5,4 +5,11 @@ The following list is the package name followed by a quick description of the as
5 5
 `isEmpty` functions.
6 6
 2. StackArray -- Implement `Stack<E>` to use an array as a stack.  You'll need to potentially grow the array in the 
7 7
 `push` method.  Do this first with an `E[]` array, and then again with an `Object[]` array.  Both should compile
8
-without warnings and pass the tests.
8
+without warnings and pass the tests.
9
+3. Table -- Implemented for you is Entry<K, V>.  
10
+Implement Table<K,V> so that it manages an ArrayList of Entry<K,V>.  You must implement
11
+    * `get` which takes a key and returns either the entry from the ArrayList with that key, or null if none is found.
12
+    * `put` which takes a key and value and sets the value in the ArrayList to Entry(key, value);
13
+        * Remember, a key point to exactly one value
14
+    * `remove` which takes a key and removes it from the ArrayList if it's in there.  It's a void method; no return type.
15
+4.

+ 20
- 0
src/main/java/Table/Entry.java ファイルの表示

@@ -0,0 +1,20 @@
1
+package Table;
2
+
3
+public class Entry<K, V> {
4
+    private K key;
5
+    private V value;
6
+
7
+    public Entry(K key, V value) {
8
+        this.key = key;
9
+        this.value = value;
10
+    }
11
+
12
+    public K getKey() {
13
+        return key;
14
+    }
15
+
16
+    public V getValue() {
17
+        return value;
18
+    }
19
+
20
+}

+ 17
- 0
src/main/java/Table/Table.java ファイルの表示

@@ -0,0 +1,17 @@
1
+package Table;
2
+
3
+import java.util.ArrayList;
4
+
5
+/**
6
+ * This class needs to manage an ArrayList of Entry objects.  It needs a get method that takes a key and returns
7
+ * its corresponding value, or null of the key is not in the arraylist.  It needs a put method that takes a key and value
8
+ * and makes an entry with key, value.  NOTE: There cannot be two entries with the same key.
9
+ * It also needs a remove method which takes a key and, if that key exists in the arraylist, removes that item.
10
+ * Void return on `remove`.
11
+ */
12
+public class Table<K, V> {
13
+    private ArrayList entries;
14
+
15
+    public Table() {
16
+    }
17
+}

+ 50
- 0
src/test/java/Table/TableTest.java ファイルの表示

@@ -0,0 +1,50 @@
1
+//package Table;
2
+//
3
+//import org.junit.Assert;
4
+//import org.junit.Test;
5
+//
6
+//public class TableTest {
7
+//    @Test
8
+//    public void testGetWithoutAnItemReturnsNull() throws Exception {
9
+//        // Given an empty table
10
+//        Table<String, Integer> table = new Table<String, Integer>();
11
+//        // When we try and get an item then it returns null
12
+//        Assert.assertEquals(table.get("foo"), null);
13
+//    }
14
+//
15
+//    @Test
16
+//    public void testPutAnItemReturnsAndDoesNotDelete() throws Exception {
17
+//        //Given an empty table
18
+//        Table<String, Integer> table = new Table<String, Integer>();
19
+//        // When we put an item in it
20
+//        table.put("foo", 1);
21
+//        // Then we should be able to get it's value
22
+//        Assert.assertEquals(table.get("foo"), new Integer(1));
23
+//        // And then we should be able to get it again as it wasn't removed
24
+//        Assert.assertEquals(table.get("foo"), new Integer(1));
25
+//    }
26
+//
27
+//    @Test
28
+//    public void testOverwritingAnItem() throws Exception {
29
+//        //Given an empty table
30
+//        Table<String, Integer> table = new Table<String, Integer>();
31
+//        // When we put an item in it
32
+//        table.put("foo", 1);
33
+//        // And we put a new value with the same key
34
+//        table.put("foo", 2);
35
+//        // Then we should get back the new value
36
+//        Assert.assertEquals(table.get("foo"), new Integer(2));
37
+//    }
38
+//
39
+//    @Test
40
+//    public void testRemoveAnItem() throws Exception {
41
+//        //Given an empty table
42
+//        Table<String, Integer> table = new Table<String, Integer>();
43
+//        // When we put an item in it
44
+//        table.put("foo", 1);
45
+//        // And we remove that item
46
+//        table.remove("foo");
47
+//        // Then we should get back null for that balue
48
+//        Assert.assertEquals(table.get("foo"), null);
49
+//    }
50
+//}