Kaynağa Gözat

completed part1

Kaitrina High 6 yıl önce
ebeveyn
işleme
1a03848dde

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

@@ -10,7 +10,7 @@ import java.util.Collections;
10 10
  * A max method that returns the largest item in the arraylist
11 11
  * And a minmax method that returns a pair containing the largest and smallest items from the array list
12 12
  */
13
-public class Arrays {
14
-    public static <___> Pair<E> firstLast(ArrayList<___> a) {
15
-    }
16
-}
13
+//public class Arrays {
14
+//    public static <___> Pair<E> firstLast(ArrayList<___> a) {
15
+//    }
16
+//}

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

@@ -1,5 +1,7 @@
1 1
 package StackArray;
2 2
 
3
+//import com.sun.java.util.jar.pack.ConstantPool;
4
+
3 5
 import java.util.Arrays;
4 6
 
5 7
 /**
@@ -8,8 +10,36 @@ import java.util.Arrays;
8 10
  * @param <E>
9 11
  */
10 12
 public class GenericStack<E> {
11
-    private E[] elements;
13
+
14
+    private Object[] elements;
15
+    private int top;
16
+    private final static int EMPTY = -1;
17
+    private final static int DEFAULT_Capacity = 10;
12 18
 
13 19
     public GenericStack() {
20
+        this(DEFAULT_Capacity);
21
+    }
22
+
23
+    public GenericStack(int initialCapacity) {
24
+        elements =  new Object [initialCapacity];
25
+             top = EMPTY;
26
+    }
27
+
28
+    public boolean isEmpty(){
29
+        return (top == EMPTY);
30
+    }
31
+
32
+    @SuppressWarnings("unchecked")
33
+    public E pop(){
34
+        if (top == EMPTY){
35
+            throw new IndexOutOfBoundsException();
36
+        }
37
+        return (E)elements[top--];
38
+    }
39
+
40
+    public void push(E e) {
41
+        elements[++top] = e;
14 42
     }
15 43
 }
44
+
45
+

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

@@ -1,16 +1,45 @@
1 1
 package StackArrayList;
2 2
 
3
+import java.lang.reflect.Array;
3 4
 import java.util.ArrayList;
5
+import java.util.Arrays;
4 6
 
5 7
 /**
6 8
  * Implement Stack<E> by adding the push, pop, and isEmpty functions.  It must pass the prewritten unit tests.
7 9
  * If you pop on an empty stack, throw an IndexOutOfBoundsException.
8 10
  */
9
-public class Stack<E> {
10
-    private ArrayList elements;
11
+public class Stack<E>  {
11 12
 
13
+    private ArrayList<E> elements;
12 14
 
13
-    public Stack(){
15
+    public Stack() {
16
+        this.elements = new ArrayList();
17
+    }
18
+
19
+    /**
20
+     * @return true if this list contains no elements
21
+     */
22
+    public boolean isEmpty() {
23
+        return this.elements.size() == 0;
24
+    }
14 25
 
26
+    /**
27
+     * Pushes an item onto the top of this stack. This has exactly the same effect as:
28
+     * addElement(item)
29
+     * @param item the item to be pushed onto this stack.
30
+     *   //should return the item argument.
31
+     */
32
+    public void push(E item) {
33
+        elements.add(item);
34
+    }
35
+
36
+    /*
37
+    *Removes the object at the top of this stack
38
+    *returns that object as the value of this function.
39
+     */
40
+    public E pop() throws IndexOutOfBoundsException{
41
+        return this.elements.remove(this.elements.size() - 1);
15 42
     }
16 43
 }
44
+
45
+

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