Przeglądaj źródła

completed pair minmax

Kaitrina High 6 lat temu
rodzic
commit
11639fad3c

+ 42
- 1
src/main/java/Pair/Pair.java Wyświetl plik

@@ -7,6 +7,47 @@ 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
+    private E first;
13
+    private E second;
14
+
15
+    public Pair(E first, E second) {
16
+        this.first = first;
17
+        this.second = second;
18
+    }
19
+
20
+
21
+    public E getFirst() {
22
+        return this.first;
23
+    }
24
+
25
+    public E getSecond() {
26
+        return this.second;
27
+    }
28
+
29
+    @SuppressWarnings("unchecked")
30
+    public E min() {
31
+        if (first.compareTo(second) > 0) {
32
+            return second;
33
+        }
34
+        return first;
35
+    }
36
+    @SuppressWarnings("unchecked")
37
+    public E max() {
38
+        if (first.compareTo(second) < 0) {
39
+            return second;
40
+        }
41
+        return first;
42
+    }
43
+
11 44
 
12 45
 }
46
+
47
+
48
+//Pair -- This is a multi-step one:
49
+//        Create a Pair that stores a pair of elements of type E.
50
+//        Create two methods, min and max, that return the largest and smallest of the Pair.
51
+
52
+
53
+

+ 3
- 1
src/main/java/StackArray/GenericStack.java Wyświetl plik

@@ -17,6 +17,7 @@ public class GenericStack<E> {
17 17
     private final static int DEFAULT_Capacity = 10;
18 18
 
19 19
     public GenericStack() {
20
+
20 21
         this(DEFAULT_Capacity);
21 22
     }
22 23
 
@@ -26,10 +27,11 @@ public class GenericStack<E> {
26 27
     }
27 28
 
28 29
     public boolean isEmpty(){
30
+
29 31
         return (top == EMPTY);
30 32
     }
31 33
 
32
-    @SuppressWarnings("unchecked")
34
+    @SuppressWarnings("uncheck")
33 35
     public E pop(){
34 36
         if (top == EMPTY){
35 37
             throw new IndexOutOfBoundsException();

+ 2
- 0
src/main/java/StackArray/ObjectStack.java Wyświetl plik

@@ -25,6 +25,7 @@ public class ObjectStack<E> {
25 25
     }
26 26
 
27 27
     public boolean isEmpty() {
28
+
28 29
         return (top == EMPTY);
29 30
     }
30 31
 
@@ -37,6 +38,7 @@ public class ObjectStack<E> {
37 38
     }
38 39
 
39 40
     public void push(Object o) {
41
+
40 42
         elements[++top] = o;
41 43
     }
42 44
 

+ 1
- 0
src/main/java/StackArrayList/Stack.java Wyświetl plik

@@ -13,6 +13,7 @@ public class Stack<E>  {
13 13
     private ArrayList<E> elements;
14 14
 
15 15
     public Stack() {
16
+
16 17
         this.elements = new ArrayList();
17 18
     }
18 19
 

+ 32
- 32
src/test/java/Pair/PairTest.java Wyświetl plik

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