瀏覽代碼

swap done

April Rivera 6 年之前
父節點
當前提交
9ecee3b088

+ 9
- 0
src/main/java/ArrayListCombiner/ArrayListCombiner.java 查看文件

@@ -10,4 +10,13 @@ import java.util.ArrayList;
10 10
  */
11 11
 public class ArrayListCombiner {
12 12
 
13
+
14
+
15
+    public void superCombiner(){
16
+
17
+    }
18
+    public void extendCombiner(){
19
+
20
+    }
21
+
13 22
 }

+ 1
- 0
src/main/java/Swap/Swap.java 查看文件

@@ -4,6 +4,7 @@ package Swap;
4 4
  * Keep this.  Just make it so the tests pass.
5 5
  */
6 6
 public class Swap {
7
+
7 8
     public static <T> T[] swap(int i, int j, T... values) {
8 9
         T temp = values[i];
9 10
         values[i] = values[j];

+ 25
- 7
src/main/java/Table/Table.java 查看文件

@@ -10,22 +10,40 @@ import java.util.ArrayList;
10 10
  * Void return on `remove`.
11 11
  */
12 12
 public class Table<K, V> {
13
-    private ArrayList entries;
14
-    private Entry<K, V> entry;
13
+    private ArrayList<Entry> entries;
14
+
15 15
 
16 16
     public Table() {
17
+        this.entries = new ArrayList();
18
+
17 19
     }
18 20
     public V get(K key){
19
-        if(entry.getKey() != null){
20
-            return entry.getValue();
21
+        for(Entry e : entries){
22
+            if(e.getKey().equals(key)){
23
+                return (V) e.getValue();
21 24
             }
22
-            return null;
23 25
         }
26
+        return null;
27
+    }
24 28
 
25 29
     public void put(K key, V value){
26
-
30
+        int numberOfEntries = 0;
31
+        for(Entry e : entries){
32
+            if(e.getKey().equals(key)){
33
+                entries.set(numberOfEntries, new Entry(key, value));
34
+            }
35
+            numberOfEntries++;
36
+        }
37
+        entries.add(new Entry(key, value));
27 38
     }
28
-    public void remove(K key){
29 39
 
40
+
41
+
42
+    public void remove(K key){
43
+        for(int i = 0; i < entries.size(); i++){
44
+            if(entries.get(i).getKey().equals(key)){
45
+                entries.remove(i);
46
+            }
47
+        }
30 48
     }
31 49
 }

+ 40
- 1
src/main/java/TableNested/TableNested.java 查看文件

@@ -8,4 +8,43 @@ import java.util.ArrayList;
8 8
  */
9 9
 public class TableNested<K, V> {
10 10
 
11
-}
11
+
12
+        private ArrayList<Table.Entry> entries;
13
+
14
+
15
+        public TableNested() {
16
+            this.entries = new ArrayList();
17
+
18
+        }
19
+        public V get(K key){
20
+            for(Table.Entry e : entries){
21
+                if(e.getKey().equals(key)){
22
+                    return (V) e.getValue();
23
+                }
24
+            }
25
+            return null;
26
+        }
27
+
28
+        public void put(K key, V value){
29
+            int numberOfEntries = 0;
30
+            for(Table.Entry e : entries){
31
+                if(e.getKey().equals(key)){
32
+                    entries.set(numberOfEntries, new Table.Entry(key, value));
33
+                }
34
+                numberOfEntries++;
35
+            }
36
+            entries.add(new Table.Entry(key, value));
37
+        }
38
+
39
+
40
+
41
+        public void remove(K key){
42
+            for(int i = 0; i < entries.size(); i++){
43
+                if(entries.get(i).getKey().equals(key)){
44
+                    entries.remove(i);
45
+                }
46
+            }
47
+        }
48
+    }
49
+
50
+

+ 35
- 35
src/test/java/ArrayListCombiner/ArrayListCombinerTest.java 查看文件

@@ -9,40 +9,40 @@ import org.junit.Assert;
9 9
 import java.util.ArrayList;
10 10
 
11 11
 public class ArrayListCombinerTest {
12
-//    Employee foo = new Employee("FOO", 100);
13
-//    Manager bar = new Manager("BAR", 100);
14
-//    @Test
15
-//    public void testExtendCombiner() throws Exception {
16
-//        // Given an array list with employees
17
-//        ArrayList<Employee> first = new ArrayList<>();
18
-//        first.add(foo);
19
-//        // An an array list with managers
20
-//        ArrayList<Manager> second = new ArrayList<>();
21
-//        second.add(bar);
22
-//        // When  I combine them
23
-//        ArrayListCombiner.extendCombiner(first, second);
24
-//        // Then I should get an arrayList with both
25
-//        ArrayList<Employee> expected = new ArrayList<>();
26
-//        expected.add(foo);
27
-//        expected.add(bar);
28
-//        Assert.assertEquals(expected, first);
29
-//    }
30
-//
31
-//    @Test
32
-//    public void testSuperCombiner() throws Exception {
33
-//        // Given an array list with employees
34
-//        ArrayList<Employee> first = new ArrayList<>();
35
-//        first.add(foo);
36
-//        // An an array list with managers
37
-//        ArrayList<Manager> second = new ArrayList<>();
38
-//        second.add(bar);
39
-//        // When  I combine them
40
-//        ArrayListCombiner.superCombiner(first, second);
41
-//        // Then I should get an arrayList with both
42
-//        ArrayList<Employee> expected = new ArrayList<>();
43
-//        expected.add(foo);
44
-//        expected.add(bar);
45
-//        Assert.assertEquals(expected, first);
46
-//    }
12
+    Employee foo = new Employee("FOO", 100);
13
+    Manager bar = new Manager("BAR", 100);
14
+    @Test
15
+    public void testExtendCombiner() throws Exception {
16
+        // Given an array list with employees
17
+        ArrayList<Employee> first = new ArrayList<>();
18
+        first.add(foo);
19
+        // An an array list with managers
20
+        ArrayList<Manager> second = new ArrayList<>();
21
+        second.add(bar);
22
+        // When  I combine them
23
+        ArrayListCombiner.extendCombiner(first, second);
24
+        // Then I should get an arrayList with both
25
+        ArrayList<Employee> expected = new ArrayList<>();
26
+        expected.add(foo);
27
+        expected.add(bar);
28
+        Assert.assertEquals(expected, first);
29
+    }
30
+
31
+    @Test
32
+    public void testSuperCombiner() throws Exception {
33
+        // Given an array list with employees
34
+        ArrayList<Employee> first = new ArrayList<>();
35
+        first.add(foo);
36
+        // An an array list with managers
37
+        ArrayList<Manager> second = new ArrayList<>();
38
+        second.add(bar);
39
+        // When  I combine them
40
+        ArrayListCombiner.superCombiner(first, second);
41
+        // Then I should get an arrayList with both
42
+        ArrayList<Employee> expected = new ArrayList<>();
43
+        expected.add(foo);
44
+        expected.add(bar);
45
+        Assert.assertEquals(expected, first);
46
+    }
47 47
 
48 48
 }

+ 15
- 15
src/test/java/Swap/SwapTest.java 查看文件

@@ -1,16 +1,16 @@
1 1
 package Swap;
2
-//
3
-//import org.junit.Assert;
4
-//import org.junit.Test;
5
-//
6
-///**
7
-// * Get the tests passing.
8
-// */
9
-//public class SwapTest {
10
-//    @Test
11
-//    public void testSwap() throws Exception {
12
-//        Double[] result = Swap.swap(0,1, 1.5, 2,3);
13
-//        Double[] expected = {2.0, 1.5, 3.0};
14
-//        Assert.assertArrayEquals(expected, result);
15
-//    }
16
-//}
2
+
3
+import org.junit.Assert;
4
+        import org.junit.Test;
5
+
6
+/**
7
+ * Get the tests passing.
8
+ */
9
+public class SwapTest {
10
+    @Test
11
+    public void testSwap() throws Exception {
12
+        Integer[] result = Swap.swap(0,1, 15, 2, 3);
13
+        Integer[] expected = {2, 15, 3};
14
+        Assert.assertArrayEquals(expected, result);
15
+    }
16
+}

+ 2
- 2
src/test/java/Table/TableTest.java 查看文件

@@ -44,7 +44,7 @@ public class TableTest {
44 44
         table.put("foo", 1);
45 45
         // And we remove that item
46 46
         table.remove("foo");
47
-        // Then we should get back null for that balue
47
+        // Then we should get back null for that value
48 48
         Assert.assertEquals(table.get("foo"), null);
49 49
     }
50
-}
50
+}

+ 50
- 50
src/test/java/TableNested/TableNestedTest.java 查看文件

@@ -1,50 +1,50 @@
1
-//package TableNested;
2
-//
3
-//import org.junit.Assert;
4
-//import org.junit.Test;
5
-//
6
-//public class TableNestedTest {
7
-//    @Test
8
-//    public void testGetWithoutAnItemReturnsNull() throws Exception {
9
-//        // Given an empty table
10
-//        TableNested<String, Integer> table = new TableNested<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
-//        TableNested<String, Integer> table = new TableNested<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
-//        TableNested<String, Integer> table = new TableNested<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
-//        TableNested<String, Integer> table = new TableNested<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
-//}
1
+package TableNested;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class TableNestedTest {
7
+    @Test
8
+    public void testGetWithoutAnItemReturnsNull() throws Exception {
9
+        // Given an empty table
10
+        TableNested<String, Integer> table = new TableNested<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
+        TableNested<String, Integer> table = new TableNested<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
+        TableNested<String, Integer> table = new TableNested<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
+        TableNested<String, Integer> table = new TableNested<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
+}