Procházet zdrojové kódy

Done, yay collections

April Rivera před 6 roky
rodič
revize
7428f0d598

+ 6
- 5
src/main/java/ArrayListCombiner/ArrayListCombiner.java Zobrazit soubor

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

+ 9
- 0
src/main/java/MapFunc/MapFunc.java Zobrazit soubor

@@ -9,4 +9,13 @@ import java.util.function.Function;
9 9
  */
10 10
 public class MapFunc {
11 11
 
12
+
13
+    public static <T,R> ArrayList map(ArrayList<T> list, Function<T, R> object){
14
+        ArrayList<R> newList = new ArrayList<>();
15
+        for(T t : list){
16
+        newList.add(object.apply(t));
17
+        }
18
+        return newList;
19
+    }
20
+
12 21
 }

+ 22
- 3
src/main/java/Pair/Arrays.java Zobrazit soubor

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

+ 37
- 1
src/main/java/Pair/Pair.java Zobrazit soubor

@@ -1,5 +1,6 @@
1 1
 package Pair;
2 2
 
3
+
3 4
 /**
4 5
  * You need to store two values of type `E`, set them in a constructor, and have the following methods,
5 6
  * getFirst
@@ -7,6 +8,41 @@ package Pair;
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 first;
14
+    private E second;
15
+    Pair<E> p;
16
+
17
+    public Pair(E first, E second) {
18
+
19
+        this.first = first;
20
+        this.second = second;
21
+    }
22
+
23
+    public E getFirst() {
24
+        return first;
25
+    }
26
+
27
+    public E getSecond() {
28
+        return second;
29
+    }
30
+    public E min(){
31
+        int result = compareTo(first);
32
+        boolean secondIsGreater = result == -1;
33
+        if(secondIsGreater){
34
+            return second;
35
+        }else return first;
36
+    }
11 37
 
38
+    public E max(){
39
+        int result = compareTo(first);
40
+        boolean secondIsGreater = result == 1;
41
+        if(secondIsGreater){
42
+            return first;
43
+        }else return second;
44
+    }
45
+    public int compareTo(E e){
46
+        return this.first.compareTo(e);
47
+    }
12 48
 }

+ 36
- 36
src/test/java/MapFunc/MapFuncTest.java Zobrazit soubor

@@ -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 Zobrazit soubor

@@ -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 Zobrazit soubor

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