ahsonali 6 gadus atpakaļ
vecāks
revīzija
7fb57571d7

+ 1
- 0
src/main/java/MapFunc/MapFunc.java Parādīt failu

@@ -17,6 +17,7 @@ public class MapFunc
17 17
 
18 18
      */
19 19
 
20
+    //T is the Type and R is the return type against the T
20 21
     public static<T,R> ArrayList map(ArrayList<T> inputArrayList, Function<T,R> function)
21 22
     {
22 23
            ArrayList<R> values = new ArrayList<R>();

+ 28
- 4
src/main/java/Pair/Arrays.java Parādīt failu

@@ -10,7 +10,31 @@ import java.util.Collections;
10 10
  * A max method that returns the largest item in the arraylist
11 11
  * And a minmax method that returns a pair containing the largest and smallest items from the array list
12 12
  */
13
-//public class Arrays {
14
-//    public static <___> Pair<E> firstLast(ArrayList<___> a) {
15
-//    }
16
-//}
13
+public class Arrays
14
+{
15
+    //Pair<E> is the return type
16
+    //firstLast is the name of the method
17
+    //Parameter is an ArrayList of a
18
+    public static <E extends Comparable> Pair<E> firstLast(ArrayList<E> a)
19
+    {
20
+        return new Pair(a.get(0), a.get(a.size() - 1));
21
+    }
22
+
23
+    public static <E extends Comparable> Comparable min(ArrayList<E> a)
24
+    {
25
+           return Collections.min(a);
26
+    }
27
+
28
+    public static <E extends Comparable> Comparable max(ArrayList<E> a)
29
+    {
30
+        return Collections.max(a);
31
+    }
32
+
33
+    public static <E extends Comparable> Pair minMax(ArrayList<E> a)
34
+    {
35
+        return new Pair<>(min(a), max(a));
36
+    }
37
+
38
+
39
+
40
+}

+ 40
- 1
src/main/java/Pair/Pair.java Parādīt failu

@@ -7,6 +7,45 @@ 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>
11
+{
12
+    E first;
13
+    E second;
14
+
15
+    public Pair(E first, E second)
16
+    {
17
+        this.first = first;
18
+        this.second = second;
19
+    }
20
+
21
+    public E getFirst() {
22
+        return first;
23
+    }
24
+
25
+    public E getSecond(){
26
+        return second;
27
+    }
28
+
29
+    public E min()
30
+    {
31
+        //if first is less then second it returns a negative
32
+        if(first.compareTo(second) < 0)
33
+        {
34
+            return first;
35
+        }
36
+        return second;
37
+    }
38
+
39
+    public E max()
40
+    {
41
+        if(first.compareTo(second) > 0)
42
+        {
43
+            return first;
44
+        }
45
+
46
+        return second;
47
+    }
48
+
49
+
11 50
 
12 51
 }

+ 76
- 74
src/test/java/Pair/ArraysTest.java Parādīt failu

@@ -1,74 +1,76 @@
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
+    }
28
+
29
+    @Test
30
+    public void testMin() throws Exception {
31
+        // Given an ArrayList of Integers
32
+        ArrayList<Integer> al = new ArrayList<>();
33
+        al.add(1);
34
+        al.add(5);
35
+        al.add(3);
36
+        al.add(4);
37
+        al.add(2);
38
+        al.add(0);
39
+        al.add(1000);
40
+        // When min is called assert that it gets the smallest item
41
+        Assert.assertEquals(new Integer(0), Arrays.min(al));
42
+    }
43
+
44
+    @Test
45
+    public void testMax() throws Exception {
46
+        // Given an ArrayList of Integers
47
+        ArrayList<Integer> al = new ArrayList<>();
48
+        al.add(1);
49
+        al.add(5);
50
+        al.add(3);
51
+        al.add(4);
52
+        al.add(2);
53
+        al.add(0);
54
+        al.add(1000);
55
+        // When min is called assert that it gets the largest item
56
+        Assert.assertEquals(new Integer(1000), Arrays.max(al));
57
+    }
58
+
59
+    @Test
60
+    public void testMinMax() throws Exception {
61
+        // Given an ArrayList of Integers
62
+        ArrayList<Integer> al = new ArrayList<>();
63
+        al.add(1);
64
+        al.add(5);
65
+        al.add(3);
66
+        al.add(4);
67
+        al.add(2);
68
+        al.add(0);
69
+        al.add(1000);
70
+        // When minMax is called
71
+        Pair<Integer> result = Arrays.minMax(al);
72
+        // Then it should return the first and last items
73
+        Assert.assertEquals(new Integer(0), result.min());
74
+        Assert.assertEquals(new Integer(1000), result.max());
75
+    }
76
+}

+ 32
- 32
src/test/java/Pair/PairTest.java Parādīt failu

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