Kthomas před 6 roky
rodič
revize
f5733fce55

+ 8
- 2
src/main/java/MapFunc/MapFunc.java Zobrazit soubor

@@ -7,6 +7,12 @@ import java.util.function.Function;
7 7
  * Create a function called `map` that takes an ArrayList and a `Function<T,R>` object,
8 8
  * and returns an ArrayList with all of the elements of the first after the function is applied to them.
9 9
  */
10
-public class MapFunc {
11
-
10
+public class MapFunc {//T being my input type and R being my output type
11
+    public static <T, R> ArrayList<R> map (ArrayList<T> array, Function<T, R> functor){
12
+        ArrayList<R> arrayList = new ArrayList<>();//empty arraylist
13
+        for (T thelid : array) { //iterate through my array for each of the lids of type T
14
+            arrayList.add(functor.apply(thelid));//in my new list add the function being applied
15
+        } //to each of the lids
16
+        return arrayList; //arrayList being my output type
17
+    }
12 18
 }

+ 12
- 12
src/main/java/Pair/Arrays.java Zobrazit soubor

@@ -1,15 +1,15 @@
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
- */
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 13
 //public class Arrays {
14 14
 //    public static <___> Pair<E> firstLast(ArrayList<___> a) {
15 15
 //    }

+ 44
- 2
src/main/java/Pair/Pair.java Zobrazit soubor

@@ -7,6 +7,48 @@ package Pair;
7 7
  * min -> returns the minimum of the pair
8 8
  * max -> returns the maximum of the pair
9 9
  */
10
-public class Pair {
10
+public class Pair <E extends Comparable<E>>  {
11
+    //without extending you cannot compare generic types
12
+    //by extending comparing individual values of generic types is now viable
13
+    private E firstValue;
14
+    private E secondValue;
11 15
 
12
-}
16
+    Pair(E first, E second){
17
+        this.firstValue = first;
18
+        this.secondValue = second;
19
+    }
20
+
21
+    private int compareTo(E otherValue) {
22
+        return firstValue.compareTo(otherValue);
23
+    }
24
+
25
+    public E getFirst(){
26
+        return firstValue;
27
+    }
28
+
29
+    public E getSecond(){
30
+        return secondValue;
31
+    }
32
+
33
+    public E min(){
34
+        int compareResult = compareTo(firstValue);
35
+        boolean secondValueIsGreater = compareResult == -1;
36
+
37
+        if (secondValueIsGreater) {
38
+            return secondValue;
39
+        } else {
40
+            return firstValue;
41
+        }
42
+    }
43
+
44
+    public E max(){
45
+        int compareResult = compareTo(secondValue);
46
+        boolean firstValIsGreater = compareResult == 1;
47
+
48
+        if (firstValIsGreater) {
49
+            return firstValue;
50
+        } else {
51
+            return secondValue;
52
+        }
53
+    }
54
+}

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