Przeglądaj źródła

First commit with the first few assignments added.

Zach Marcin 7 lat temu
commit
b4bbd94c26

+ 8
- 0
README.md Wyświetl plik

@@ -0,0 +1,8 @@
1
+# Generics MicroLabs
2
+The microlabs for the Generics chapter are all grouped together.  So, you need to work through them package by package.
3
+The following list is the package name followed by a quick description of the assignment.
4
+1. StackArrayList -- Implement `Stack<E>` to use an ArrayList as a stack.  You need to make the `push`, `pop`, and
5
+`isEmpty` functions.
6
+2. StackArray -- Implement `Stack<E>` to use an array as a stack.  You'll need to potentially grow the array in the 
7
+`push` method.  Do this first with an `E[]` array, and then again with an `Object[]` array.  Both should compile
8
+without warnings and pass the tests.

+ 15
- 0
src/main/java/StackArray/GenericStack.java Wyświetl plik

@@ -0,0 +1,15 @@
1
+package StackArray;
2
+
3
+import java.util.Arrays;
4
+
5
+/**
6
+ * Expand the ArrayList implementation of stack here to use an E[] 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 GenericStack<E> {
11
+    private E[] elements;
12
+
13
+    public GenericStack() {
14
+    }
15
+}

+ 16
- 0
src/main/java/StackArray/ObjectStack.java Wyświetl plik

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

+ 16
- 0
src/main/java/StackArrayList/Stack.java Wyświetl plik

@@ -0,0 +1,16 @@
1
+package StackArrayList;
2
+
3
+import java.util.ArrayList;
4
+
5
+/**
6
+ * Implement Stack<E> by adding the push, pop, and isEmpty functions.  It must pass the prewritten unit tests.
7
+ * If you pop on an empty stack, throw an IndexOutOfBoundsException.
8
+ */
9
+public class Stack<E> {
10
+    private ArrayList elements;
11
+
12
+
13
+    public Stack(){
14
+
15
+    }
16
+}

+ 41
- 0
src/test/java/StackArray/GenericStackTest.java Wyświetl plik

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

+ 39
- 0
src/test/java/StackArray/ObjectStackTest.java Wyświetl plik

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

+ 45
- 0
src/test/java/StackArrayList/StackTest.java Wyświetl plik

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