Jessica Campbell пре 6 година
родитељ
комит
4f4c5c5fe4

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

+ 35
- 16
src/main/java/Pair/Arrays.java Прегледај датотеку

@@ -1,16 +1,35 @@
1
-//package Pair;
2
-//
3
-//import java.util.ArrayList;
4
-//import java.util.Collections;
5
-//
6
-///**
7
-// * In here you must make firstLast, which will return a pair of the first element in the array list and the last
8
-// * element in the arraylist.
9
-// * You must also make a min method that returns the smallest item in the array list
10
-// * 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
-// */
13
-//public class Arrays {
14
-//    public static <___> Pair<E> firstLast(ArrayList<___> a) {
15
-//    }
16
-//}
1
+package Pair;
2
+import java.util.ArrayList;
3
+import java.util.Collections;
4
+
5
+/**
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
+ * element in the arraylist.
9
+ * You must also make a min method that returns the smallest item
10
+ * in the array list
11
+ * A max method that returns the largest item in the arraylist
12
+ * And a minmax method that returns a pair containing the largest
13
+ * and smallest items from the array list
14
+ */
15
+public class Arrays {
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);
34
+    }
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
 }

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