Katrice Williams-Dredden 6 年 前
コミット
60130a81a0
共有4 個のファイルを変更した101 個の追加77 個の削除を含む
  1. 16
    16
      src/main/java/Pair/Arrays.java
  2. 20
    16
      src/main/java/StackArray/ObjectStack.java
  3. 20
    0
      src/main/java/StackArrayList/Stack.java
  4. 45
    45
      src/test/java/StackArrayList/StackTest.java

+ 16
- 16
src/main/java/Pair/Arrays.java ファイルの表示

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

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

@@ -1,16 +1,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
-
15
-    }
16
-}
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
+//}

+ 20
- 0
src/main/java/StackArrayList/Stack.java ファイルの表示

@@ -7,10 +7,30 @@ 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<E>();
16
+    }
17
+
18
+    public void push(E value){
19
+        //pushes adds an element
20
+        this.elements.add(value);
21
+    }
22
+
23
+    public E pop(){
24
+        //removes an element
25
+        return (E) elements.remove(this.elements.size()-1);
26
+    }
27
+
28
+    public boolean isEmpty(){
29
+        //Checks to see is the elements array is empty, if its equal to 0 its empty.
30
+        return (elements.size()==0);
31
+    }
14 32
 
33
+    public E pop() {
34
+        return (E) this.elements;
15 35
     }
16 36
 }

+ 45
- 45
src/test/java/StackArrayList/StackTest.java ファイルの表示

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