Bläddra i källkod

Everything extending Stack works, combiner stubs

vvmk 6 år sedan
förälder
incheckning
21de74bab5

+ 5
- 0
src/main/java/ArrayListCombiner/ArrayListCombiner.java Visa fil

9
  * The second method should be called superCombiner and should use ? super E
9
  * The second method should be called superCombiner and should use ? super E
10
  */
10
  */
11
 public class ArrayListCombiner {
11
 public class ArrayListCombiner {
12
+    public static <E> void superCombiner(ArrayList<? super E> parent, ArrayList<E> child) {
13
+    }
14
+
15
+    public static <E> void extendCombiner(ArrayList<E> parent, ArrayList<? extends E> child) {
16
+    }
12
 }
17
 }

+ 16
- 2
src/main/java/Pair/Arrays.java Visa fil

1
 package Pair;
1
 package Pair;
2
 
2
 
3
 import java.util.ArrayList;
3
 import java.util.ArrayList;
4
-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 of the first element in the array list and the last
11
  * And a minmax method that returns a pair containing the largest and smallest items from the array list
10
  * And a minmax method that returns a pair containing the largest and smallest items from the array list
12
  */
11
  */
13
 public class Arrays {
12
 public class Arrays {
14
-    public static <E> Pair<E> firstLast(ArrayList<?> a) {
13
+    public static <E> Pair<E> firstLast(ArrayList<E> a) {
14
+        E first = a.get(0);
15
+        E second = a.get(a.size() - 1);
16
+
17
+        return new Pair<>(first, second);
18
+    }
19
+
20
+    public <E> E max(ArrayList<E> a) {
21
+        return null;
22
+    }
23
+
24
+    public <E> E min(ArrayList<E> a) {
25
+        return null;
26
+    }
27
+
28
+    public <E> Pair<E> minmax(ArrayList<E> a) {
15
         return null;
29
         return null;
16
     }
30
     }
17
 }
31
 }

+ 22
- 0
src/main/java/Pair/Pair.java Visa fil

8
  * max -> returns the maximum of the pair
8
  * max -> returns the maximum of the pair
9
  */
9
  */
10
 public class Pair<E> {
10
 public class Pair<E> {
11
+    private E first;
12
+    private E second;
11
 
13
 
14
+    public Pair(E first, E second) {
15
+        this.first = first;
16
+        this.second = second;
17
+    }
18
+
19
+    public E getFirst() {
20
+        return first;
21
+    }
22
+
23
+    public E getSecond() {
24
+        return second;
25
+    }
26
+
27
+    public E min() {
28
+        return null;
29
+    }
30
+
31
+    public E max() {
32
+        return null;
33
+    }
12
 }
34
 }

+ 1
- 3
src/main/java/StackArray/ObjectStack.java Visa fil

11
 public class ObjectStack<E> extends Stack<E> {
11
 public class ObjectStack<E> extends Stack<E> {
12
     private Object[] elements;
12
     private Object[] elements;
13
 
13
 
14
-    public ObjectStack() {
15
-
16
-    }
14
+    public ObjectStack() {}
17
 }
15
 }

+ 37
- 37
src/test/java/ArrayListCombiner/ArrayListCombinerTest.java Visa fil

2
 
2
 
3
 import Employee.Employee;
3
 import Employee.Employee;
4
 import Employee.Manager;
4
 import Employee.Manager;
5
-import org.junit.Test;
6
-
7
 import org.junit.Assert;
5
 import org.junit.Assert;
6
+import org.junit.Test;
8
 
7
 
9
 import java.util.ArrayList;
8
 import java.util.ArrayList;
10
 
9
 
11
 public class ArrayListCombinerTest {
10
 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
-//    }
11
+    Employee foo = new Employee("FOO", 100);
12
+    Manager bar = new Manager("BAR", 100);
13
+
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
 }