Katrice Williams-Dredden před 6 roky
rodič
revize
75c1c78872

+ 26
- 0
src/main/java/StackArray/GenericStack.java Zobrazit soubor

@@ -11,5 +11,31 @@ public class GenericStack<E> {
11 11
     private E[] elements;
12 12
 
13 13
     public GenericStack() {
14
+        this.elements = (E[]) new Object[0];
15
+    }
16
+
17
+    public E push(E value){
18
+        //needs to remove an element from E[] and resize
19
+        //now I just need to push the value onto the end of the array
20
+        E[] newArray = Arrays.copyOf(this.elements, this.elements.length + 1);
21
+        newArray[newArray.length-1] = value;
22
+        this.elements = newArray;
23
+        return value;
24
+
25
+    }
26
+
27
+    public E pop(){
28
+        //according to the api, pop should return a value
29
+        //didn't need to assign the value
30
+        E value = this.elements[elements.length-1];
31
+        E[] newArray = Arrays.copyOf(elements, elements.length - 1);
32
+        //newArray[newArray.length-1] = value;
33
+        this.elements = newArray;
34
+        return value;
35
+    }
36
+
37
+    public boolean isEmpty(){
38
+        return (elements.length==0);
39
+
14 40
     }
15 41
 }

+ 35
- 20
src/main/java/StackArray/ObjectStack.java Zobrazit soubor

@@ -1,20 +1,35 @@
1
-//package StackArray;
2
-//
3
-//import java.util.Arrays;
4
-//
5
-///**
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.
8
-// * @param <E>
9
-// */
10
-//public class ObjectStack<E> {
11
-//    private Object[] elements;
12
-//
13
-//    public ObjectStack() {
14
-//        this.elements = new Object[];
15
-//    }
16
-//    //the way stacks work, its just like generics accept you are using Objects
17
-//    public Object push(Object value){
18
-//
19
-//    }
20
-//}
1
+package StackArray;
2
+
3
+import java.util.Arrays;
4
+
5
+/**
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.
8
+ * @param <E>
9
+ */
10
+public class ObjectStack<E> {
11
+    private Object[] elements;
12
+
13
+    public ObjectStack() {
14
+        this.elements = new Object[0];
15
+    }
16
+    //the way stacks work, its just like generics accept you are using Objects
17
+    public Object push(Object value){
18
+        Object[] newArray = Arrays.copyOf(this.elements, this.elements.length + 1);
19
+        newArray[newArray.length-1] = value;
20
+        this.elements = newArray;
21
+        return value;
22
+    }
23
+
24
+    public Object pop(){
25
+        Object value = this.elements[elements.length-1];
26
+        Object[] newArray = Arrays.copyOf(elements, elements.length - 1);
27
+        //newArray[newArray.length-1] = value;
28
+        this.elements = newArray;
29
+        return value;
30
+    }
31
+
32
+    public boolean isEmpty(){
33
+        return(elements.length==0);
34
+    }
35
+}

+ 1
- 3
src/main/java/StackArrayList/Stack.java Zobrazit soubor

@@ -30,7 +30,5 @@ public class Stack<E> {
30 30
         return (elements.size()==0);
31 31
     }
32 32
 
33
-    public E pop() {
34
-        return (E) this.elements;
35
-    }
33
+
36 34
 }

+ 41
- 41
src/test/java/StackArray/GenericStackTest.java Zobrazit soubor

@@ -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 Zobrazit soubor

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