Keith Brinker 6 lat temu
rodzic
commit
70956e6a63

+ 20
- 1
src/main/java/StackArray/ObjectStack.java Wyświetl plik

@@ -9,8 +9,27 @@ import java.util.Arrays;
9 9
  */
10 10
 public class ObjectStack<E> {
11 11
     private Object[] elements;
12
-
12
+    Integer actualSize = 0;
13 13
     public ObjectStack() {
14
+        this.elements =  new Object[2];
15
+    }
16
+
17
+
18
+    public void push(E element) {
19
+        Object[] newArr = Arrays.copyOf(this.elements, this.elements.length + 1);
20
+        newArr[actualSize++] = element;
21
+        this.elements = newArr;
22
+
23
+    }
24
+
25
+    public boolean isEmpty() {
26
+        return actualSize == 0;
27
+    }
14 28
 
29
+    public Object pop() {
30
+        Object element = this.elements[actualSize - 1];
31
+        this.elements[actualSize - 1] = null;
32
+        actualSize--;
33
+        return element;
15 34
     }
16 35
 }

+ 8
- 0
src/main/java/Table/Entry.java Wyświetl plik

@@ -17,4 +17,12 @@ public class Entry<K, V> {
17 17
         return value;
18 18
     }
19 19
 
20
+    public void setKey(K key){
21
+        this.key = key;
22
+    }
23
+
24
+    public void setValue(V value){
25
+        this.value = value;
26
+    }
27
+
20 28
 }

+ 40
- 10
src/main/java/Table/Table.java Wyświetl plik

@@ -2,16 +2,46 @@ package Table;
2 2
 
3 3
 import java.util.ArrayList;
4 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;
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<Entry> entries;
14 14
 
15
-    public Table() {
15
+        public Table() {
16
+            this.entries = new ArrayList<Entry>();
17
+        }
18
+
19
+    public V get(K key) {
20
+            for (int i = 0; i < entries.size(); i++){
21
+                if (entries.get(i).getKey().equals(key)){
22
+                    return (V)entries.get(i).getValue();
23
+                }
24
+
25
+            }
26
+            return null;
27
+    }
28
+
29
+    public void put(K key, V value) {
30
+    for (int i =0; i < entries.size(); i++){
31
+       if (entries.get(i).getKey().equals(key)){
32
+           System.out.println("Error, already exists");
33
+       }
34
+       entries.get(i).setKey(key);
35
+       entries.get(i).setValue(value);
36
+    }
37
+    entries.add(new Entry(key, value));
38
+    }
39
+
40
+    public void remove(K key) {
41
+    for (int i = 0; i < entries.size(); i++){
42
+        if (entries.get(i).getKey().equals(key)){
43
+            entries.remove(i);
44
+        }
45
+    }
16 46
     }
17 47
 }

+ 39
- 39
src/test/java/StackArray/ObjectStackTest.java Wyświetl plik

@@ -1,39 +1,39 @@
1
-//package StackArray;
2
-//
3
-//import org.junit.Assert;
4
-//import org.junit.Test;
5
-//
6
-//public class ObjectStackTest {
7
-//    @Test
8
-//    public void testPushingGrowsTheStack() throws Exception {
9
-//        // Given an empty stack
10
-//        ObjectStack<String> stack = new ObjectStack<>();
11
-//        // Assert that it is empty.
12
-//        Assert.assertEquals(true, stack.isEmpty());
13
-//        // When we push something onto the stack
14
-//        stack.push("foobar");
15
-//        // Then it shouldn't be empty
16
-//        Assert.assertEquals(false, stack.isEmpty());
17
-//    }
18
-//
19
-//    @Test
20
-//    public void testPushingAndPoppingOrder() throws Exception {
21
-//        // Given an empty stack
22
-//        ObjectStack<String> stack = new ObjectStack<>();
23
-//        // When we push two elements on it
24
-//        stack.push("foo");
25
-//        stack.push("bar");
26
-//        // Then we should see them returned in the correct order
27
-//        Assert.assertEquals("bar", stack.pop());
28
-//        Assert.assertEquals("foo", stack.pop());
29
-//    }
30
-//
31
-//    @Test(expected = IndexOutOfBoundsException.class)
32
-//    public void testPopFirst() throws Exception {
33
-//        // Given an empty stack
34
-//        ObjectStack<String> stack = new ObjectStack<>();
35
-//        // When it's popped
36
-//        stack.pop();
37
-//        // Then we should get an exception
38
-//    }
39
-//}
1
+package StackArray;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class ObjectStackTest {
7
+    @Test
8
+    public void testPushingGrowsTheStack() throws Exception {
9
+        // Given an empty stack
10
+        ObjectStack<String> stack = new ObjectStack<>();
11
+        // Assert that it is empty.
12
+        Assert.assertEquals(true, stack.isEmpty());
13
+        // When we push something onto the stack
14
+        stack.push("foobar");
15
+        // Then it shouldn't be empty
16
+        Assert.assertEquals(false, stack.isEmpty());
17
+    }
18
+
19
+    @Test
20
+    public void testPushingAndPoppingOrder() throws Exception {
21
+        // Given an empty stack
22
+        ObjectStack<String> stack = new ObjectStack<>();
23
+        // When we push two elements on it
24
+        stack.push("foo");
25
+        stack.push("bar");
26
+        // Then we should see them returned in the correct order
27
+        Assert.assertEquals("bar", stack.pop());
28
+        Assert.assertEquals("foo", stack.pop());
29
+    }
30
+
31
+    @Test(expected = IndexOutOfBoundsException.class)
32
+    public void testPopFirst() throws Exception {
33
+        // Given an empty stack
34
+        ObjectStack<String> stack = new ObjectStack<>();
35
+        // When it's popped
36
+        stack.pop();
37
+        // Then we should get an exception
38
+    }
39
+}

+ 15
- 15
src/test/java/Swap/SwapTest.java Wyświetl plik

@@ -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
+        Double[] result = Swap.swap(0,1, 1.5, 2.0,3.0);
13
+        Double[] expected = {2.0, 1.5, 3.0};
14
+        Assert.assertArrayEquals(expected, result);
15
+    }
16
+}

+ 50
- 50
src/test/java/Table/TableTest.java Wyświetl plik

@@ -1,50 +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
-//}
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
+}