Eric Cordell 6 years ago
parent
commit
7c091d5af8

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

5
 /**
5
 /**
6
  * Expand the ArrayList implementation of stack here to use an E[] array.  Still implement push, pop, and isEmpty.
6
  * Expand the ArrayList implementation of stack here to use an E[] array.  Still implement push, pop, and isEmpty.
7
  * Remember, you might need to resize the stack in the push method.
7
  * Remember, you might need to resize the stack in the push method.
8
+ *
8
  * @param <E>
9
  * @param <E>
9
  */
10
  */
10
 public class GenericStack<E> {
11
 public class GenericStack<E> {
11
     private E[] elements;
12
     private E[] elements;
12
 
13
 
13
     public GenericStack() {
14
     public GenericStack() {
15
+        this.elements = (E[]) new Object[0];
16
+    }
17
+
18
+
19
+    public void push(E value) {
20
+        E[] newArray = Arrays.copyOf(elements, elements.length + 1);
21
+        newArray[newArray.length - 1] = value;
22
+        this.elements = newArray;
23
+    }
24
+
25
+
26
+    public E pop() {
27
+        E value = elements[elements.length - 1];
28
+        E[] newArray = Arrays.copyOf(elements, elements.length - 1);
29
+        this.elements = newArray;
30
+        return value;
31
+
32
+    }
33
+
34
+
35
+    public boolean isEmpty() {
36
+        if (elements.length == 0) {
37
+            return true;
38
+        }
39
+        return false;
14
     }
40
     }
15
 }
41
 }

+ 24
- 0
src/main/java/StackArray/ObjectStack.java View File

5
 /**
5
 /**
6
  * Expand the ArrayList implementation of stack here to use an Object[] array.  Still implement push, pop, and isEmpty.
6
  * Expand the ArrayList implementation of stack here to use an Object[] array.  Still implement push, pop, and isEmpty.
7
  * Remember, you might need to resize the stack in the push method.
7
  * Remember, you might need to resize the stack in the push method.
8
+ *
8
  * @param <E>
9
  * @param <E>
9
  */
10
  */
10
 public class ObjectStack<E> {
11
 public class ObjectStack<E> {
11
     private Object[] elements;
12
     private Object[] elements;
12
 
13
 
13
     public ObjectStack() {
14
     public ObjectStack() {
15
+        this.elements = new Object[0];
16
+    }
17
+
18
+    public void push(Object value) {
19
+        Object[] newArray = Arrays.copyOf(elements, elements.length + 1);
20
+        newArray[newArray.length - 1] = value;
21
+        this.elements = newArray;
22
+    }
23
+
24
+
25
+    public Object pop() {
26
+        Object value = elements[elements.length - 1];
27
+        Object[] newArray = Arrays.copyOf(elements, elements.length - 1);
28
+        this.elements = newArray;
29
+        return value;
30
+
31
+    }
32
+
14
 
33
 
34
+    public boolean isEmpty() {
35
+        if (elements.length == 0) {
36
+            return true;
37
+        }
38
+        return false;
15
     }
39
     }
16
 }
40
 }

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

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 View File

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