Bladeren bron

part 1 & 2

Katrice Williams-Dredden 6 jaren geleden
bovenliggende
commit
60130a81a0

+ 16
- 16
src/main/java/Pair/Arrays.java Bestand weergeven

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 Bestand weergeven

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 Bestand weergeven

7
  * If you pop on an empty stack, throw an IndexOutOfBoundsException.
7
  * If you pop on an empty stack, throw an IndexOutOfBoundsException.
8
  */
8
  */
9
 public class Stack<E> {
9
 public class Stack<E> {
10
+
10
     private ArrayList elements;
11
     private ArrayList elements;
11
 
12
 
12
 
13
 
13
     public Stack(){
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 Bestand weergeven

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