瀏覽代碼

Merge 4f4c5c5fe483b84e580eca3b12758cb2f0e7eb19 into 844bbe32418e760e4607b01fa37541c39badf9aa

Jessica Campbell 6 年之前
父節點
當前提交
2f59dfc537
沒有帳戶連結到提交者的電子郵件

+ 16
- 1
src/main/java/ArrayListCombiner/ArrayListCombiner.java 查看文件

@@ -3,10 +3,25 @@ package ArrayListCombiner;
3 3
 import java.util.ArrayList;
4 4
 
5 5
 /**
6
- * Create two generic methods that take two arraylists.  The methods should both append the second ArrayList's items,
6
+ * Create two generic methods that take two arraylists.  The methods should both
7
+ * append the second ArrayList's items,
7 8
  * to the first.  Use a wildcard for one of the type arguments in each method.
8 9
  * The first method should be called extendCombiner and should use ? extends E
9 10
  * The second method should be called superCombiner and should use ? super E
10 11
  */
11 12
 public class ArrayListCombiner {
13
+
14
+    public ArrayListCombiner() {
15
+
16
+    }
17
+    public static <E> ArrayList extendCombiner (ArrayList<E> first, ArrayList<? extends E> second){
18
+        first.addAll(second);
19
+        return first;
20
+    }
21
+
22
+    public static <E> ArrayList superCombiner (ArrayList<? super  E> first, ArrayList<E> second){
23
+        first.addAll(second);
24
+        return first;
25
+    }
26
+
12 27
 }

+ 15
- 3
src/main/java/MapFunc/MapFunc.java 查看文件

@@ -4,9 +4,21 @@ import java.util.ArrayList;
4 4
 import java.util.function.Function;
5 5
 
6 6
 /**
7
- * Create a function called `map` that takes an ArrayList and a `Function<T,R>` object,
8
- * and returns an ArrayList with all of the elements of the first after the function is applied to them.
7
+ * Create a function called `map` that takes an ArrayList and a
8
+ * `Function<T,R>` object,
9
+ * and returns an ArrayList with all of the elements of the first
10
+ * after the function is applied to them.
9 11
  */
10 12
 public class MapFunc {
11
-
13
+// type R bc it could return a different type but could be the same
14
+    public static <T, R> ArrayList map(ArrayList<T> input, Function<T, R> myFunction) {
15
+        //create new arraylist
16
+        ArrayList<R> mappedArrayList = new ArrayList<>();
17
+        for (int i = 0; i < input.size(); i++) {
18
+            R element = myFunction.apply(input.get(i));
19
+            mappedArrayList.add(element);
20
+        }
21
+        return mappedArrayList;
22
+    }
12 23
 }
24
+

+ 24
- 5
src/main/java/Pair/Arrays.java 查看文件

@@ -1,16 +1,35 @@
1 1
 package Pair;
2
-
3 2
 import java.util.ArrayList;
4 3
 import java.util.Collections;
5 4
 
6 5
 /**
7
- * In here you must make firstLast, which will return a pair of the first element in the array list and the last
6
+ * In here you must make firstLast, which will return a pair
7
+ * of the first element in the array list and the last
8 8
  * element in the arraylist.
9
- * You must also make a min method that returns the smallest item in the array list
9
+ * You must also make a min method that returns the smallest item
10
+ * in the array list
10 11
  * A max method that returns the largest item in the arraylist
11
- * And a minmax method that returns a pair containing the largest and smallest items from the array list
12
+ * And a minmax method that returns a pair containing the largest
13
+ * and smallest items from the array list
12 14
  */
13 15
 public class Arrays {
14
-    public static <___> Pair<E> firstLast(ArrayList<___> a) {
16
+    public static <E extends Comparable> Pair<E> firstLast(ArrayList<E> a) {
17
+        return new Pair<E>(a.get(0), a.get(a.size()-1));
18
+    }
19
+
20
+    public static <E extends Comparable> E min(ArrayList<E> arrayList){
21
+        Collections.sort(arrayList);
22
+        return arrayList.get(0);
23
+    }
24
+
25
+    public static <E extends Comparable> E max(ArrayList<E> arrayList){
26
+        Collections.sort(arrayList);
27
+        return arrayList.get(arrayList.size() - 1);
28
+    }
29
+
30
+    public static <E extends Comparable> Pair<E> minMax (ArrayList<E> arrayList){
31
+        Collections.sort(arrayList);
32
+        //return new Pair<E>(arrayList.get(0), arrayList.get(arrayList.size() - 1));
33
+        return firstLast(arrayList);
15 34
     }
16 35
 }

+ 33
- 2
src/main/java/Pair/Pair.java 查看文件

@@ -1,12 +1,43 @@
1 1
 package Pair;
2 2
 
3 3
 /**
4
- * You need to store two values of type `E`, set them in a constructor, and have the following methods,
4
+ * You need to store two values of type `E`, set them in a constructor, and
5
+ * have the following methods,
5 6
  * getFirst
6 7
  * getSecond
7 8
  * min -> returns the minimum of the pair
8 9
  * max -> returns the maximum of the pair
9 10
  */
10
-public class Pair {
11
+public class Pair<E extends Comparable> {
12
+
13
+    private E firstValue;
14
+    private E secondValue;
15
+
16
+    public Pair(E first, E second){
17
+        this.firstValue = first;
18
+        this.secondValue = second;
19
+    }
20
+
21
+    public E getFirst(){
22
+        return this.firstValue;
23
+    }
24
+
25
+    public E getSecond(){
26
+        return this.secondValue;
27
+    }
28
+    public E min(){
29
+        if (firstValue.compareTo(secondValue) == 1){
30
+            return secondValue;
31
+        }
32
+        return firstValue;
33
+    }
34
+
35
+    public E max(){
36
+        if (firstValue.compareTo(secondValue) == -1){
37
+            return secondValue;
38
+        }
39
+        return firstValue;
40
+    }
41
+
11 42
 
12 43
 }

+ 14
- 0
src/main/java/StackArray/GenericStack.java 查看文件

@@ -11,5 +11,19 @@ public class GenericStack<E> {
11 11
     private E[] elements;
12 12
 
13 13
     public GenericStack() {
14
+        elements = (E[]) new Object[0];
14 15
     }
16
+    public void push(E element){
17
+        elements = Arrays.copyOf(elements, elements.length +1);
18
+        elements[elements.length - 1] = element;
19
+    }
20
+    public E pop(){
21
+        E temp = elements[elements.length-1];
22
+        elements = Arrays.copyOf(elements, elements.length - 1);
23
+        return temp;
24
+    }
25
+    public boolean isEmpty(){
26
+        return elements.length == 0;
27
+    }
28
+
15 29
 }

+ 14
- 0
src/main/java/StackArray/ObjectStack.java 查看文件

@@ -11,6 +11,20 @@ public class ObjectStack<E> {
11 11
     private Object[] elements;
12 12
 
13 13
     public ObjectStack() {
14
+        elements = new Object[0];
14 15
 
15 16
     }
17
+
18
+    public void push(Object elementToAdd){
19
+        elements = Arrays.copyOf(elements, elements.length+1);
20
+        elements[elements.length - 1] = elementToAdd;
21
+    }
22
+    public Object pop(){
23
+        Object temp = elements[elements.length -1];
24
+        elements = Arrays.copyOf(elements, elements.length - 1);
25
+        return temp;
26
+    }
27
+    public boolean isEmpty(){
28
+        return elements.length == 0;
29
+    }
16 30
 }

+ 16
- 1
src/main/java/StackArrayList/Stack.java 查看文件

@@ -10,7 +10,22 @@ public class Stack<E> {
10 10
     private ArrayList elements;
11 11
 
12 12
 
13
-    public Stack(){
13
+    public Stack() {
14
+        this.elements = new ArrayList<E>();
15
+    }
16
+
17
+    public E pop() {
18
+        E lastElement;
19
+        lastElement = (E) elements.get(elements.size() - 1);
20
+        elements.remove(lastElement);
21
+        return lastElement;
22
+    }
23
+
24
+    public void push(E elementToAdd) {
25
+        elements.add(elementToAdd);
26
+    }
14 27
 
28
+    public Boolean isEmpty() {
29
+        return elements.size() == 0;
15 30
     }
16 31
 }

+ 46
- 1
src/main/java/Table/Table.java 查看文件

@@ -1,6 +1,7 @@
1 1
 package Table;
2 2
 
3 3
 import java.util.ArrayList;
4
+import java.util.Map;
4 5
 
5 6
 /**
6 7
  * This class needs to manage an ArrayList of Entry objects.  It needs a get method that takes a key and returns
@@ -10,8 +11,52 @@ import java.util.ArrayList;
10 11
  * Void return on `remove`.
11 12
  */
12 13
 public class Table<K, V> {
13
-    private ArrayList entries;
14
+    private ArrayList<Entry> entries;
15
+
14 16
 
15 17
     public Table() {
18
+        entries = new ArrayList<Entry>();
19
+    }
20
+
21
+    public V get(K key) {
22
+        for (int i = 0; i < entries.size(); i++) {
23
+            if (entries.get(i).getKey().equals(key)) {
24
+                return (V) entries.get(i).getValue();
25
+            }
26
+        }
27
+        return null;
28
+    }
29
+
30
+    public boolean containsKey(K key) {
31
+        for (int i = 0; i < entries.size(); i++) {
32
+            if (entries.get(i).getKey().equals(key)) {
33
+                return true;
34
+            }
35
+        }
36
+        return false;
16 37
     }
38
+
39
+    public void put(K key, V value) {
40
+        if (containsKey(key)) {
41
+            for (int i = 0; i < entries.size(); i++) {
42
+                if (entries.get(i).getKey().equals(key)) {
43
+                    entries.remove(i);
44
+                    entries.add(i, new Entry(key, value));
45
+                }
46
+            }
47
+        } else {
48
+            entries.add(new Entry(key, value));
49
+        }
50
+    }
51
+
52
+    public void remove(K key) {
53
+        if (containsKey(key)) {
54
+            for (int i = 0; i < entries.size(); i++) {
55
+                if (entries.get(i).getKey().equals(key)) {
56
+                    entries.remove(i);
57
+                }
58
+            }
59
+        }
60
+    }
61
+
17 62
 }

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

@@ -1,11 +1,95 @@
1 1
 package TableNested;
2 2
 
3
+import Table.Entry;
4
+
3 5
 import java.util.ArrayList;
4 6
 
5 7
 /**
6
- * All you need to do for this microlab is take the Table and Entry from the last one and make Entry a nested class.
8
+ * All you need to do for this microlab is take the Table and Entry from
9
+ * the last one and make Entry a nested class.
7 10
  * Think about how nested classes should work with generics.
8 11
  */
9 12
 public class TableNested<K, V> {
10 13
 
14
+    private ArrayList<Entry> entries;
15
+
16
+    public TableNested() {
17
+        entries = new ArrayList<Entry>();
18
+    }
19
+    //Entry class within TableNested Class
20
+     class Entry<K, V> {
21
+        private K key;
22
+        private V value;
23
+
24
+        public Entry(K key, V value) {
25
+            this.key = key;
26
+            this.value = value;
27
+        }
28
+
29
+        public K getKey() {
30
+
31
+            return key;
32
+        }
33
+
34
+        public V getValue() {
35
+
36
+            return value;
37
+        }
38
+
39
+    }
40
+    //end of nested class
41
+
42
+    public V get(K key) {
43
+        for (int i = 0; i < entries.size(); i++) {
44
+            if (entries.get(i).getKey().equals(key)) {
45
+                return (V) entries.get(i).getValue();
46
+            }
47
+        }
48
+        return null;
49
+    }
50
+
51
+    public boolean containsKey(K key) {
52
+        for (int i = 0; i < entries.size(); i++) {
53
+            if (entries.get(i).getKey().equals(key)) {
54
+                return true;
55
+            }
56
+        }
57
+        return false;
58
+    }
59
+
60
+    public void put(K key, V value) {
61
+        if (containsKey(key)) {
62
+            int index = entries.indexOf(getEntry(key));
63
+            if (entries.get(index).getKey().equals(key)) {
64
+                entries.remove(index);
65
+                entries.add(index, new Entry<>(key, value));
66
+            }
67
+        }
68
+        else {
69
+            entries.add(new Entry<>(key, value));
70
+        }
71
+    }
72
+
73
+    public void remove(K key) {
74
+        if (containsKey(key)) {
75
+            for (int i = 0; i < entries.size(); i++) {
76
+                if (entries.get(i).getKey().equals(key)) {
77
+                    entries.remove(i);
78
+                }
79
+            }
80
+        }
81
+    }
82
+    public Entry getEntry(K key) {
83
+        for (int i = 0; i < entries.size(); i++) {
84
+            if (entries.get(i).getKey().equals(key)) {
85
+                return entries.get(i);
86
+            }
87
+        }
88
+        return null;
89
+    }
90
+
11 91
 }
92
+
93
+
94
+
95
+

+ 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
 }

+ 36
- 36
src/test/java/MapFunc/MapFuncTest.java 查看文件

@@ -1,36 +1,36 @@
1
-//package MapFunc;
2
-//
3
-//import MapFunc.MapFunc;
4
-//import org.junit.Test;
5
-//
6
-//import java.util.ArrayList;
7
-//import org.junit.Assert;
8
-//
9
-//public class MapFuncTest {
10
-//    @Test
11
-//    public void testSingleTypeMap() throws Exception {
12
-//        // Given an integer array list
13
-//        ArrayList<Integer> intList = new ArrayList<>();
14
-//        intList.add(1);
15
-//        intList.add(2);
16
-//        // When it's mapped with a function to double the value
17
-//        ArrayList<Integer> mappedList = MapFunc.map(intList, num -> num*2);
18
-//        // Then all the values are doubled
19
-//        Assert.assertEquals(new Integer(2), mappedList.get(0));
20
-//        Assert.assertEquals(new Integer(4), mappedList.get(1));
21
-//    }
22
-//
23
-//    @Test
24
-//    public void testMultipleTypeMap() throws Exception {
25
-//        // Given an integer array list
26
-//        ArrayList<Integer> intList = new ArrayList<>();
27
-//        intList.add(1);
28
-//        intList.add(2);
29
-//        // When it's mapped with to string
30
-//        ArrayList<String> mappedList = MapFunc.map(intList, num -> num.toString());
31
-//        // Then all the values are doubled
32
-//        Assert.assertEquals("1", mappedList.get(0));
33
-//        Assert.assertEquals("2", mappedList.get(1));
34
-//    }
35
-//
36
-//}
1
+package MapFunc;
2
+
3
+import MapFunc.MapFunc;
4
+import org.junit.Test;
5
+
6
+import java.util.ArrayList;
7
+import org.junit.Assert;
8
+
9
+public class MapFuncTest {
10
+    @Test
11
+    public void testSingleTypeMap() throws Exception {
12
+        // Given an integer array list
13
+        ArrayList<Integer> intList = new ArrayList<>();
14
+        intList.add(1);
15
+        intList.add(2);
16
+        // When it's mapped with a function to double the value
17
+        ArrayList<Integer> mappedList = MapFunc.map(intList, num -> num*2);
18
+        // Then all the values are doubled
19
+        Assert.assertEquals(new Integer(2), mappedList.get(0));
20
+        Assert.assertEquals(new Integer(4), mappedList.get(1));
21
+    }
22
+
23
+    @Test
24
+    public void testMultipleTypeMap() throws Exception {
25
+        // Given an integer array list
26
+        ArrayList<Integer> intList = new ArrayList<>();
27
+        intList.add(1);
28
+        intList.add(2);
29
+        // When it's mapped with to string
30
+        ArrayList<String> mappedList = MapFunc.map(intList, num -> num.toString());
31
+        // Then all the values are doubled
32
+        Assert.assertEquals("1", mappedList.get(0));
33
+        Assert.assertEquals("2", mappedList.get(1));
34
+    }
35
+
36
+}

+ 74
- 74
src/test/java/Pair/ArraysTest.java 查看文件

@@ -1,74 +1,74 @@
1
-//package Pair;
2
-//
3
-//import org.junit.Assert;
4
-//import org.junit.Test;
5
-//
6
-//import java.util.ArrayList;
7
-//
8
-//public class ArraysTest {
9
-//    @Test
10
-//    public void firstLast() throws Exception {
11
-//        // Given an ArrayList of Integers
12
-//        ArrayList<Integer> al = new ArrayList<>();
13
-//        al.add(1);
14
-//        al.add(5);
15
-//        al.add(3);
16
-//        al.add(4);
17
-//        al.add(2);
18
-//        al.add(0);
19
-//        al.add(1000);
20
-//        // When firstLast is called
21
-//        Pair<Integer> result = Arrays.firstLast(al);
22
-//        // Then it should return the first and last items
23
-//        Assert.assertEquals(new Integer(1), result.getFirst());
24
-//        Assert.assertEquals(new Integer(1000), result.getSecond());
25
-//    }
26
-//
27
-//    @Test
28
-//    public void testMin() throws Exception {
29
-//        // Given an ArrayList of Integers
30
-//        ArrayList<Integer> al = new ArrayList<>();
31
-//        al.add(1);
32
-//        al.add(5);
33
-//        al.add(3);
34
-//        al.add(4);
35
-//        al.add(2);
36
-//        al.add(0);
37
-//        al.add(1000);
38
-//        // When min is called assert that it gets the smallest item
39
-//        Assert.assertEquals(new Integer(0), Arrays.min(al));
40
-//    }
41
-//
42
-//    @Test
43
-//    public void testMax() throws Exception {
44
-//        // Given an ArrayList of Integers
45
-//        ArrayList<Integer> al = new ArrayList<>();
46
-//        al.add(1);
47
-//        al.add(5);
48
-//        al.add(3);
49
-//        al.add(4);
50
-//        al.add(2);
51
-//        al.add(0);
52
-//        al.add(1000);
53
-//        // When min is called assert that it gets the largest item
54
-//        Assert.assertEquals(new Integer(1000), Arrays.max(al));
55
-//    }
56
-//
57
-//    @Test
58
-//    public void testMinMax() throws Exception {
59
-//        // Given an ArrayList of Integers
60
-//        ArrayList<Integer> al = new ArrayList<>();
61
-//        al.add(1);
62
-//        al.add(5);
63
-//        al.add(3);
64
-//        al.add(4);
65
-//        al.add(2);
66
-//        al.add(0);
67
-//        al.add(1000);
68
-//        // When minMax is called
69
-//        Pair<Integer> result = Arrays.minMax(al);
70
-//        // Then it should return the first and last items
71
-//        Assert.assertEquals(new Integer(0), result.min());
72
-//        Assert.assertEquals(new Integer(1000), result.max());
73
-//    }
74
-//}
1
+package Pair;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.util.ArrayList;
7
+
8
+public class ArraysTest {
9
+    @Test
10
+    public void firstLast() throws Exception {
11
+        // Given an ArrayList of Integers
12
+        ArrayList<Integer> al = new ArrayList<>();
13
+        al.add(1);
14
+        al.add(5);
15
+        al.add(3);
16
+        al.add(4);
17
+        al.add(2);
18
+        al.add(0);
19
+        al.add(1000);
20
+        // When firstLast is called
21
+        Pair<Integer> result = Arrays.firstLast(al);
22
+        // Then it should return the first and last items
23
+        Assert.assertEquals(new Integer(1), result.getFirst());
24
+        Assert.assertEquals(new Integer(1000), result.getSecond());
25
+    }
26
+
27
+    @Test
28
+    public void testMin() throws Exception {
29
+        // Given an ArrayList of Integers
30
+        ArrayList<Integer> al = new ArrayList<>();
31
+        al.add(1);
32
+        al.add(5);
33
+        al.add(3);
34
+        al.add(4);
35
+        al.add(2);
36
+        al.add(0);
37
+        al.add(1000);
38
+        // When min is called assert that it gets the smallest item
39
+        Assert.assertEquals(new Integer(0), Arrays.min(al));
40
+    }
41
+
42
+    @Test
43
+    public void testMax() throws Exception {
44
+        // Given an ArrayList of Integers
45
+        ArrayList<Integer> al = new ArrayList<>();
46
+        al.add(1);
47
+        al.add(5);
48
+        al.add(3);
49
+        al.add(4);
50
+        al.add(2);
51
+        al.add(0);
52
+        al.add(1000);
53
+        // When min is called assert that it gets the largest item
54
+        Assert.assertEquals(new Integer(1000), Arrays.max(al));
55
+    }
56
+
57
+    @Test
58
+    public void testMinMax() throws Exception {
59
+        // Given an ArrayList of Integers
60
+        ArrayList<Integer> al = new ArrayList<>();
61
+        al.add(1);
62
+        al.add(5);
63
+        al.add(3);
64
+        al.add(4);
65
+        al.add(2);
66
+        al.add(0);
67
+        al.add(1000);
68
+        // When minMax is called
69
+        Pair<Integer> result = Arrays.minMax(al);
70
+        // Then it should return the first and last items
71
+        Assert.assertEquals(new Integer(0), result.min());
72
+        Assert.assertEquals(new Integer(1000), result.max());
73
+    }
74
+}

+ 32
- 32
src/test/java/Pair/PairTest.java 查看文件

@@ -1,32 +1,32 @@
1
-//package Pair;
2
-//
3
-//import org.junit.Test;
4
-//import org.junit.Assert;
5
-//
6
-//public class PairTest {
7
-//
8
-//    @Test
9
-//    public void testGetters() throws Exception {
10
-//        // Given a pair with "Foo" and "Bar"
11
-//        Pair<String> p = new Pair<String>("Foo", "Bar");
12
-//        // When getFirst and getSecond are called, they should be returned.
13
-//        Assert.assertEquals("Foo", p.getFirst());
14
-//        Assert.assertEquals("Bar",  p.getSecond());
15
-//    }
16
-//
17
-//    @Test
18
-//    public void testMin() throws Exception {
19
-//        // Given a pair with two values
20
-//        Pair<Double> p = new Pair<Double>(1.23, 2.34);
21
-//        // When p.min() is called, the smallest should be returned.
22
-//        Assert.assertEquals(new Double(1.23), p.min());
23
-//    }
24
-//
25
-//    @Test
26
-//    public void testMax() throws Exception {
27
-//        // Given a pair with two values
28
-//        Pair<Double> p = new Pair<Double>(1.23, 2.34);
29
-//        // When p.max() is called, the largest should be returned.
30
-//        Assert.assertEquals(new Double(2.34), p.max());
31
-//    }
32
-//}
1
+package Pair;
2
+
3
+import org.junit.Test;
4
+import org.junit.Assert;
5
+
6
+public class PairTest {
7
+
8
+    @Test
9
+    public void testGetters() throws Exception {
10
+        // Given a pair with "Foo" and "Bar"
11
+        Pair<String> p = new Pair<String>("Foo", "Bar");
12
+        // When getFirst and getSecond are called, they should be returned.
13
+        Assert.assertEquals("Foo", p.getFirst());
14
+        Assert.assertEquals("Bar",  p.getSecond());
15
+    }
16
+
17
+    @Test
18
+    public void testMin() throws Exception {
19
+        // Given a pair with two values
20
+        Pair<Double> p = new Pair<Double>(1.23, 2.34);
21
+        // When p.min() is called, the smallest should be returned.
22
+        Assert.assertEquals(new Double(1.23), p.min());
23
+    }
24
+
25
+    @Test
26
+    public void testMax() throws Exception {
27
+        // Given a pair with two values
28
+        Pair<Double> p = new Pair<Double>(1.23, 2.34);
29
+        // When p.max() is called, the largest should be returned.
30
+        Assert.assertEquals(new Double(2.34), p.max());
31
+    }
32
+}

+ 41
- 41
src/test/java/StackArray/GenericStackTest.java 查看文件

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

+ 39
- 39
src/test/java/StackArray/ObjectStackTest.java 查看文件

@@ -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
+}

+ 45
- 45
src/test/java/StackArrayList/StackTest.java 查看文件

@@ -1,45 +1,45 @@
1
-//package StackArrayList;
2
-//
3
-//import org.junit.Test;
4
-//
5
-//import org.junit.Assert;
6
-//
7
-//public class StackTest {
8
-//    @Test
9
-//    public void testEmptyStackStopsBeingEmptyWhenAnItemIsAdded() throws Exception {
10
-//        // Given an empty stack
11
-//        Stack<String> stack = new Stack<>();
12
-//        // Assert that it starts empty
13
-//        Assert.assertEquals(true, stack.isEmpty());
14
-//        // When an element gets pushed
15
-//        stack.push("foobar");
16
-//        // Then the stack should not be empty.
17
-//        Assert.assertEquals(false, stack.isEmpty());
18
-//    }
19
-//
20
-//    @Test
21
-//    public void testTwoItemsPushedComeOutInCorrectOrder() throws Exception {
22
-//        // Given an empty stack
23
-//        Stack<String> stack = new Stack<>();
24
-//
25
-//        //When two items are pushed
26
-//        stack.push("foo");
27
-//        stack.push("bar");
28
-//
29
-//        // Then they should come off in reverse order.
30
-//        Assert.assertEquals("bar", stack.pop());
31
-//        Assert.assertEquals("foo", stack.pop());
32
-//
33
-//        // And then the stack should be empty
34
-//        Assert.assertEquals(true, stack.isEmpty());
35
-//    }
36
-//
37
-//    @Test(expected = IndexOutOfBoundsException.class)
38
-//    public void testPopFirst() throws Exception {
39
-//        // Given an empty stack
40
-//        Stack<String> stack = new Stack<>();
41
-//        // Then it is popped
42
-//        stack.pop();
43
-//        // We should get an exception
44
-//    }
45
-//}
1
+package StackArrayList;
2
+
3
+import org.junit.Test;
4
+
5
+import org.junit.Assert;
6
+
7
+public class StackTest {
8
+    @Test
9
+    public void testEmptyStackStopsBeingEmptyWhenAnItemIsAdded() throws Exception {
10
+        // Given an empty stack
11
+        Stack<String> stack = new Stack<>();
12
+        // Assert that it starts empty
13
+        Assert.assertEquals(true, stack.isEmpty());
14
+        // When an element gets pushed
15
+        stack.push("foobar");
16
+        // Then the stack should not be empty.
17
+        Assert.assertEquals(false, stack.isEmpty());
18
+    }
19
+
20
+    @Test
21
+    public void testTwoItemsPushedComeOutInCorrectOrder() throws Exception {
22
+        // Given an empty stack
23
+        Stack<String> stack = new Stack<>();
24
+
25
+        //When two items are pushed
26
+        stack.push("foo");
27
+        stack.push("bar");
28
+
29
+        // Then they should come off in reverse order.
30
+        Assert.assertEquals("bar", stack.pop());
31
+        Assert.assertEquals("foo", stack.pop());
32
+
33
+        // And then the stack should be empty
34
+        Assert.assertEquals(true, stack.isEmpty());
35
+    }
36
+
37
+    @Test(expected = IndexOutOfBoundsException.class)
38
+    public void testPopFirst() throws Exception {
39
+        // Given an empty stack
40
+        Stack<String> stack = new Stack<>();
41
+        // Then it is popped
42
+        stack.pop();
43
+        // We should get an exception
44
+    }
45
+}

+ 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
+        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 查看文件

@@ -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
+}

+ 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
+}