Kaynağa Gözat

Stack tests passing

Amy Gill 6 yıl önce
ebeveyn
işleme
951a81eec8

+ 16
- 16
src/main/java/Pair/Arrays.java Dosyayı Görüntüle

@@ -1,16 +1,16 @@
1
-package Pair;
2
-
3
-import java.util.ArrayList;
4
-import java.util.Collections;
5
-
6
-/**
7
- * In here you must make firstLast, which will return a pair of the first element in the array list and the last
8
- * element in the arraylist.
9
- * You must also make a min method that returns the smallest item in the array list
10
- * A max method that returns the largest item in the arraylist
11
- * And a minmax method that returns a pair containing the largest and smallest items from the array list
12
- */
13
-public class Arrays {
14
-    public static <___> Pair<E> firstLast(ArrayList<___> a) {
15
-    }
16
-}
1
+//package Pair;
2
+//
3
+//import java.util.ArrayList;
4
+//import java.util.Collections;
5
+//
6
+///**
7
+// * In here you must make firstLast, which will return a pair of the first element in the array list and the last
8
+// * element in the arraylist.
9
+// * You must also make a min method that returns the smallest item in the array list
10
+// * A max method that returns the largest item in the arraylist
11
+// * And a minmax method that returns a pair containing the largest and smallest items from the array list
12
+// */
13
+//public class Arrays {
14
+//    public static <___> Pair<E> firstLast(ArrayList<___> a) {
15
+//    }
16
+//}

+ 28
- 28
src/main/java/StackArray/GenericStack.java Dosyayı Görüntüle

@@ -1,28 +1,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
-    }
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
+//    }
18
+//
19
+//    public E isEmpty() {
20
+//
21
+//    }
22
+//
23
+//    public void push(E foobar) {
24
+//    }
25
+//
26
+//    public E pop() {
27
+//    }
28
+//}

+ 19
- 0
src/main/java/StackArrayList/Stack.java Dosyayı Görüntüle

@@ -7,10 +7,29 @@ import java.util.ArrayList;
7 7
  * If you pop on an empty stack, throw an IndexOutOfBoundsException.
8 8
  */
9 9
 public class Stack<E> {
10
+
10 11
     private ArrayList elements;
11 12
 
12 13
 
13 14
     public Stack(){
15
+        this.elements = new ArrayList();
16
+    }
17
+
18
+    public boolean isEmpty() {
19
+        boolean answer = false;
20
+        answer = elements.size()==0;
21
+
22
+        return answer;
23
+    }
24
+
25
+    public void push(E foobar) {
26
+        elements.add(foobar);
27
+
28
+    }
14 29
 
30
+    public E pop() {
31
+        E lastElement = (E) elements.get(elements.size()-1);
32
+        elements.remove(elements.size()-1);
33
+        return lastElement;
15 34
     }
16 35
 }

+ 41
- 41
src/test/java/StackArray/GenericStackTest.java Dosyayı Görüntüle

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

+ 45
- 45
src/test/java/StackArrayList/StackTest.java Dosyayı Görüntüle

@@ -1,45 +1,45 @@
1
-//package StackArrayList;
2
-//
3
-//import org.junit.Test;
4
-//
5
-//import org.junit.Assert;
6
-//
7
-//public class StackTest {
8
-//    @Test
9
-//    public void testEmptyStackStopsBeingEmptyWhenAnItemIsAdded() throws Exception {
10
-//        // Given an empty stack
11
-//        Stack<String> stack = new Stack<>();
12
-//        // Assert that it starts empty
13
-//        Assert.assertEquals(true, stack.isEmpty());
14
-//        // When an element gets pushed
15
-//        stack.push("foobar");
16
-//        // Then the stack should not be empty.
17
-//        Assert.assertEquals(false, stack.isEmpty());
18
-//    }
19
-//
20
-//    @Test
21
-//    public void testTwoItemsPushedComeOutInCorrectOrder() throws Exception {
22
-//        // Given an empty stack
23
-//        Stack<String> stack = new Stack<>();
24
-//
25
-//        //When two items are pushed
26
-//        stack.push("foo");
27
-//        stack.push("bar");
28
-//
29
-//        // Then they should come off in reverse order.
30
-//        Assert.assertEquals("bar", stack.pop());
31
-//        Assert.assertEquals("foo", stack.pop());
32
-//
33
-//        // And then the stack should be empty
34
-//        Assert.assertEquals(true, stack.isEmpty());
35
-//    }
36
-//
37
-//    @Test(expected = IndexOutOfBoundsException.class)
38
-//    public void testPopFirst() throws Exception {
39
-//        // Given an empty stack
40
-//        Stack<String> stack = new Stack<>();
41
-//        // Then it is popped
42
-//        stack.pop();
43
-//        // We should get an exception
44
-//    }
45
-//}
1
+package StackArrayList;
2
+
3
+import org.junit.Test;
4
+
5
+import org.junit.Assert;
6
+
7
+public class StackTest {
8
+    @Test
9
+    public void testEmptyStackStopsBeingEmptyWhenAnItemIsAdded() throws Exception {
10
+        // Given an empty stack
11
+        Stack<String> stack = new Stack<>();
12
+        // Assert that it starts empty
13
+        Assert.assertEquals(true, stack.isEmpty());
14
+        // When an element gets pushed
15
+        stack.push("foobar");
16
+        // Then the stack should not be empty.
17
+        Assert.assertEquals(false, stack.isEmpty());
18
+    }
19
+
20
+    @Test
21
+    public void testTwoItemsPushedComeOutInCorrectOrder() throws Exception {
22
+        // Given an empty stack
23
+        Stack<String> stack = new Stack<>();
24
+
25
+        //When two items are pushed
26
+        stack.push("foo");
27
+        stack.push("bar");
28
+
29
+        // Then they should come off in reverse order.
30
+        Assert.assertEquals("bar", stack.pop());
31
+        Assert.assertEquals("foo", stack.pop());
32
+
33
+        // And then the stack should be empty
34
+        Assert.assertEquals(true, stack.isEmpty());
35
+    }
36
+
37
+    @Test(expected = IndexOutOfBoundsException.class)
38
+    public void testPopFirst() throws Exception {
39
+        // Given an empty stack
40
+        Stack<String> stack = new Stack<>();
41
+        // Then it is popped
42
+        stack.pop();
43
+        // We should get an exception
44
+    }
45
+}