ソースを参照

completed part2

Kaitrina High 6 年 前
コミット
8a3e0f4d20
共有2 個のファイルを変更した70 個の追加39 個の削除を含む
  1. 31
    0
      src/main/java/StackArray/ObjectStack.java
  2. 39
    39
      src/test/java/StackArray/ObjectStackTest.java

+ 31
- 0
src/main/java/StackArray/ObjectStack.java ファイルの表示

@@ -5,12 +5,43 @@ import java.util.Arrays;
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 11
 public class ObjectStack<E> {
12
+
11 13
     private Object[] elements;
14
+    private int top;
15
+    private final static int EMPTY = -1;
16
+    private final static int DEFAULT_Capacity = 10;
12 17
 
13 18
     public ObjectStack() {
19
+        this(DEFAULT_Capacity);
20
+    }
21
+
22
+    public ObjectStack(int initialCapacity) {
23
+        elements = new Object[initialCapacity];
24
+        top = EMPTY;
25
+    }
14 26
 
27
+    public boolean isEmpty() {
28
+        return (top == EMPTY);
15 29
     }
30
+
31
+    @SuppressWarnings("unchecked")
32
+    public Object pop() {
33
+        if (top == EMPTY) {
34
+            throw new IndexOutOfBoundsException();
35
+        }
36
+        return elements[top--];
37
+    }
38
+
39
+    public void push(Object o) {
40
+        elements[++top] = o;
41
+    }
42
+
16 43
 }
44
+
45
+
46
+
47
+

+ 39
- 39
src/test/java/StackArray/ObjectStackTest.java ファイルの表示

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