Browse Source

making more progress

Keith Brinker 6 years ago
parent
commit
699869738a

+ 23
- 0
src/main/java/StackArray/GenericStack.java View File

@@ -1,15 +1,38 @@
1 1
 package StackArray;
2 2
 
3
+import java.lang.reflect.Array;
3 4
 import java.util.Arrays;
4 5
 
5 6
 /**
6 7
  * Expand the ArrayList implementation of stack here to use an E[] array.  Still implement push, pop, and isEmpty.
7 8
  * Remember, you might need to resize the stack in the push method.
9
+ *
8 10
  * @param <E>
9 11
  */
10 12
 public class GenericStack<E> {
11 13
     private E[] elements;
14
+    Integer actualSize = 0;
12 15
 
13 16
     public GenericStack() {
17
+        this.elements = (E[]) new Object[2];
18
+
19
+    }
20
+
21
+    public void push(E element) {
22
+        E[] newArr = Arrays.copyOf(this.elements, this.elements.length + 1);
23
+        newArr[actualSize++] = element;
24
+        this.elements = newArr;
25
+
26
+    }
27
+
28
+    public boolean isEmpty() {
29
+        return actualSize == 0;
30
+    }
31
+
32
+    public E pop() {
33
+        E element = this.elements[actualSize - 1];
34
+        this.elements[actualSize - 1] = null;
35
+        actualSize--;
36
+        return element;
14 37
     }
15 38
 }

+ 41
- 41
src/test/java/StackArray/GenericStackTest.java View File

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