Amy Gill пре 6 година
родитељ
комит
e961d9d2eb
2 измењених фајлова са 82 додато и 69 уклоњено
  1. 41
    28
      src/main/java/StackArray/GenericStack.java
  2. 41
    41
      src/test/java/StackArray/GenericStackTest.java

+ 41
- 28
src/main/java/StackArray/GenericStack.java Прегледај датотеку

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

+ 41
- 41
src/test/java/StackArray/GenericStackTest.java Прегледај датотеку

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