Selaa lähdekoodia

StackArrayList green

vvmk 6 vuotta sitten
vanhempi
commit
164dcce18d

+ 2
- 1
src/main/java/Pair/Arrays.java Näytä tiedosto

@@ -11,6 +11,7 @@ import java.util.Collections;
11 11
  * And a minmax method that returns a pair containing the largest and smallest items from the array list
12 12
  */
13 13
 public class Arrays {
14
-    public static <___> Pair<E> firstLast(ArrayList<___> a) {
14
+    public static <E> Pair<E> firstLast(ArrayList<?> a) {
15
+        return null;
15 16
     }
16 17
 }

+ 1
- 1
src/main/java/Pair/Pair.java Näytä tiedosto

@@ -7,6 +7,6 @@ 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> {
11 11
 
12 12
 }

+ 3
- 2
src/main/java/StackArray/GenericStack.java Näytä tiedosto

@@ -1,13 +1,14 @@
1 1
 package StackArray;
2 2
 
3
-import java.util.Arrays;
3
+import StackArrayList.Stack;
4 4
 
5 5
 /**
6 6
  * Expand the ArrayList implementation of stack here to use an E[] array.  Still implement push, pop, and isEmpty.
7 7
  * Remember, you might need to resize the stack in the push method.
8
+ *
8 9
  * @param <E>
9 10
  */
10
-public class GenericStack<E> {
11
+public class GenericStack<E> extends Stack<E> {
11 12
     private E[] elements;
12 13
 
13 14
     public GenericStack() {

+ 3
- 2
src/main/java/StackArray/ObjectStack.java Näytä tiedosto

@@ -1,13 +1,14 @@
1 1
 package StackArray;
2 2
 
3
-import java.util.Arrays;
3
+import StackArrayList.Stack;
4 4
 
5 5
 /**
6 6
  * Expand the ArrayList implementation of stack here to use an Object[] array.  Still implement push, pop, and isEmpty.
7 7
  * Remember, you might need to resize the stack in the push method.
8
+ *
8 9
  * @param <E>
9 10
  */
10
-public class ObjectStack<E> {
11
+public class ObjectStack<E> extends Stack<E> {
11 12
     private Object[] elements;
12 13
 
13 14
     public ObjectStack() {

+ 14
- 1
src/main/java/StackArrayList/Stack.java Näytä tiedosto

@@ -1,16 +1,29 @@
1 1
 package StackArrayList;
2 2
 
3 3
 import java.util.ArrayList;
4
+import java.util.List;
4 5
 
5 6
 /**
6 7
  * Implement Stack<E> by adding the push, pop, and isEmpty functions.  It must pass the prewritten unit tests.
7 8
  * If you pop on an empty stack, throw an IndexOutOfBoundsException.
8 9
  */
9 10
 public class Stack<E> {
10
-    private ArrayList elements;
11
+    private List<E> elements;
11 12
 
12 13
 
13 14
     public Stack(){
15
+        elements = new ArrayList<>();
16
+    }
17
+
18
+    public void push(E e) {
19
+        elements.add(e);
20
+    }
21
+
22
+    public E pop() {
23
+        return elements.remove(elements.size()-1);
24
+    }
14 25
 
26
+    public boolean isEmpty() {
27
+        return elements.size() == 0;
15 28
     }
16 29
 }

+ 41
- 41
src/test/java/StackArray/GenericStackTest.java Näytä tiedosto

@@ -1,41 +1,41 @@
1
-//package StackArray;
2
-//
3
-//import org.junit.Assert;
4
-//import org.junit.Test;
5
-//
6
-//public class GenericStackTest {
7
-//    @Test
8
-//    public void testPushingGrowsTheStack() throws Exception {
9
-//        // Given an empty stack
10
-//        GenericStack<String> stack = new GenericStack<>();
11
-//
12
-//        // Assert that it is empty.
13
-//        Assert.assertEquals(true, stack.isEmpty());
14
-//        // When we push something onto the stack
15
-//        stack.push("foobar");
16
-//        // Then it shouldn't be empty
17
-//        Assert.assertEquals(false, stack.isEmpty());
18
-//    }
19
-//
20
-//    @Test
21
-//    public void testPushingAndPoppingOrder() throws Exception {
22
-//        // Given an empty stack
23
-//        GenericStack<String> stack = new GenericStack<>();
24
-//        // When we push two elements on it
25
-//        stack.push("foo");
26
-//        stack.push("bar");
27
-//        // Then we should see them returned in the correct order
28
-//        Assert.assertEquals("bar", stack.pop());
29
-//        Assert.assertEquals("foo", stack.pop());
30
-//    }
31
-//
32
-//    @Test(expected = IndexOutOfBoundsException.class)
33
-//    public void testPopFirst() throws Exception {
34
-//        // Given an empty stack
35
-//        GenericStack<String> stack = new GenericStack<>();
36
-//        // When it's popped
37
-//        stack.pop();
38
-//        // Then we should get an exception
39
-//    }
40
-//
41
-//}
1
+package StackArray;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class GenericStackTest {
7
+    @Test
8
+    public void testPushingGrowsTheStack() throws Exception {
9
+        // Given an empty stack
10
+        GenericStack<String> stack = new GenericStack<>();
11
+
12
+        // Assert that it is empty.
13
+        Assert.assertEquals(true, stack.isEmpty());
14
+        // When we push something onto the stack
15
+        stack.push("foobar");
16
+        // Then it shouldn't be empty
17
+        Assert.assertEquals(false, stack.isEmpty());
18
+    }
19
+
20
+    @Test
21
+    public void testPushingAndPoppingOrder() throws Exception {
22
+        // Given an empty stack
23
+        GenericStack<String> stack = new GenericStack<>();
24
+        // When we push two elements on it
25
+        stack.push("foo");
26
+        stack.push("bar");
27
+        // Then we should see them returned in the correct order
28
+        Assert.assertEquals("bar", stack.pop());
29
+        Assert.assertEquals("foo", stack.pop());
30
+    }
31
+
32
+    @Test(expected = IndexOutOfBoundsException.class)
33
+    public void testPopFirst() throws Exception {
34
+        // Given an empty stack
35
+        GenericStack<String> stack = new GenericStack<>();
36
+        // When it's popped
37
+        stack.pop();
38
+        // Then we should get an exception
39
+    }
40
+
41
+}

+ 39
- 39
src/test/java/StackArray/ObjectStackTest.java Näytä tiedosto

@@ -1,39 +1,39 @@
1
-//package StackArray;
2
-//
3
-//import org.junit.Assert;
4
-//import org.junit.Test;
5
-//
6
-//public class ObjectStackTest {
7
-//    @Test
8
-//    public void testPushingGrowsTheStack() throws Exception {
9
-//        // Given an empty stack
10
-//        ObjectStack<String> stack = new ObjectStack<>();
11
-//        // Assert that it is empty.
12
-//        Assert.assertEquals(true, stack.isEmpty());
13
-//        // When we push something onto the stack
14
-//        stack.push("foobar");
15
-//        // Then it shouldn't be empty
16
-//        Assert.assertEquals(false, stack.isEmpty());
17
-//    }
18
-//
19
-//    @Test
20
-//    public void testPushingAndPoppingOrder() throws Exception {
21
-//        // Given an empty stack
22
-//        ObjectStack<String> stack = new ObjectStack<>();
23
-//        // When we push two elements on it
24
-//        stack.push("foo");
25
-//        stack.push("bar");
26
-//        // Then we should see them returned in the correct order
27
-//        Assert.assertEquals("bar", stack.pop());
28
-//        Assert.assertEquals("foo", stack.pop());
29
-//    }
30
-//
31
-//    @Test(expected = IndexOutOfBoundsException.class)
32
-//    public void testPopFirst() throws Exception {
33
-//        // Given an empty stack
34
-//        ObjectStack<String> stack = new ObjectStack<>();
35
-//        // When it's popped
36
-//        stack.pop();
37
-//        // Then we should get an exception
38
-//    }
39
-//}
1
+package StackArray;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class ObjectStackTest {
7
+    @Test
8
+    public void testPushingGrowsTheStack() throws Exception {
9
+        // Given an empty stack
10
+        ObjectStack<String> stack = new ObjectStack<>();
11
+        // Assert that it is empty.
12
+        Assert.assertEquals(true, stack.isEmpty());
13
+        // When we push something onto the stack
14
+        stack.push("foobar");
15
+        // Then it shouldn't be empty
16
+        Assert.assertEquals(false, stack.isEmpty());
17
+    }
18
+
19
+    @Test
20
+    public void testPushingAndPoppingOrder() throws Exception {
21
+        // Given an empty stack
22
+        ObjectStack<String> stack = new ObjectStack<>();
23
+        // When we push two elements on it
24
+        stack.push("foo");
25
+        stack.push("bar");
26
+        // Then we should see them returned in the correct order
27
+        Assert.assertEquals("bar", stack.pop());
28
+        Assert.assertEquals("foo", stack.pop());
29
+    }
30
+
31
+    @Test(expected = IndexOutOfBoundsException.class)
32
+    public void testPopFirst() throws Exception {
33
+        // Given an empty stack
34
+        ObjectStack<String> stack = new ObjectStack<>();
35
+        // When it's popped
36
+        stack.pop();
37
+        // Then we should get an exception
38
+    }
39
+}

+ 44
- 45
src/test/java/StackArrayList/StackTest.java Näytä tiedosto

@@ -1,45 +1,44 @@
1
-//package StackArrayList;
2
-//
3
-//import org.junit.Test;
4
-//
5
-//import org.junit.Assert;
6
-//
7
-//public class StackTest {
8
-//    @Test
9
-//    public void testEmptyStackStopsBeingEmptyWhenAnItemIsAdded() throws Exception {
10
-//        // Given an empty stack
11
-//        Stack<String> stack = new Stack<>();
12
-//        // Assert that it starts empty
13
-//        Assert.assertEquals(true, stack.isEmpty());
14
-//        // When an element gets pushed
15
-//        stack.push("foobar");
16
-//        // Then the stack should not be empty.
17
-//        Assert.assertEquals(false, stack.isEmpty());
18
-//    }
19
-//
20
-//    @Test
21
-//    public void testTwoItemsPushedComeOutInCorrectOrder() throws Exception {
22
-//        // Given an empty stack
23
-//        Stack<String> stack = new Stack<>();
24
-//
25
-//        //When two items are pushed
26
-//        stack.push("foo");
27
-//        stack.push("bar");
28
-//
29
-//        // Then they should come off in reverse order.
30
-//        Assert.assertEquals("bar", stack.pop());
31
-//        Assert.assertEquals("foo", stack.pop());
32
-//
33
-//        // And then the stack should be empty
34
-//        Assert.assertEquals(true, stack.isEmpty());
35
-//    }
36
-//
37
-//    @Test(expected = IndexOutOfBoundsException.class)
38
-//    public void testPopFirst() throws Exception {
39
-//        // Given an empty stack
40
-//        Stack<String> stack = new Stack<>();
41
-//        // Then it is popped
42
-//        stack.pop();
43
-//        // We should get an exception
44
-//    }
45
-//}
1
+package StackArrayList;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class StackTest {
7
+    @Test
8
+    public void testEmptyStackStopsBeingEmptyWhenAnItemIsAdded() throws Exception {
9
+        // Given an empty stack
10
+        Stack<String> stack = new Stack<>();
11
+        // Assert that it starts empty
12
+        Assert.assertEquals(true, stack.isEmpty());
13
+        // When an element gets pushed
14
+        stack.push("foobar");
15
+        // Then the stack should not be empty.
16
+        Assert.assertEquals(false, stack.isEmpty());
17
+    }
18
+
19
+    @Test
20
+    public void testTwoItemsPushedComeOutInCorrectOrder() throws Exception {
21
+        // Given an empty stack
22
+        Stack<String> stack = new Stack<>();
23
+
24
+        //When two items are pushed
25
+        stack.push("foo");
26
+        stack.push("bar");
27
+
28
+        // Then they should come off in reverse order.
29
+        Assert.assertEquals("bar", stack.pop());
30
+        Assert.assertEquals("foo", stack.pop());
31
+
32
+        // And then the stack should be empty
33
+        Assert.assertEquals(true, stack.isEmpty());
34
+    }
35
+
36
+    @Test(expected = IndexOutOfBoundsException.class)
37
+    public void testPopFirst() throws Exception {
38
+        // Given an empty stack
39
+        Stack<String> stack = new Stack<>();
40
+        // Then it is popped
41
+        stack.pop();
42
+        // We should get an exception
43
+    }
44
+}