Created
October 30, 2012 08:08
-
-
Save fikovnik/3978900 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package generics; | |
import java.util.Arrays; | |
import java.util.NoSuchElementException; | |
/** | |
* This class represents an implementation of a stack datastructure. | |
* Unfortunatelly this version does not support generics and that makes it too | |
* difficult to use. Your task is to: | |
* | |
* <ol> | |
* <li>Make it generic | |
* <li>Implement and test all the missing methods | |
* </ol> | |
* | |
* For more information on genetics go to an <a | |
* href="http://docs.oracle.com/javase/tutorial/java/generics/index.html" | |
* >official tutorial</a>. | |
* | |
*/ | |
public class Stack { | |
private static final int DEFAULT_INIT_CAPACITY = 2; | |
private Object[] elements; | |
private int size = 0; | |
public Stack() { | |
this(DEFAULT_INIT_CAPACITY); | |
} | |
public Stack(Stack that) { | |
elements = Arrays.copyOf(that.elements, that.elements.length); | |
size = that.size; | |
} | |
public Stack(int capacity) { | |
elements = new Object[capacity]; | |
} | |
public Object push(Object o) { | |
ensureCapacity(); | |
elements[size++] = o; | |
return o; | |
} | |
public Object pop() { | |
if (size == 0) { | |
throw new NoSuchElementException(); | |
} | |
Object e = elements[--size]; | |
elements[size] = null; // eliminate obsolete reference | |
return e; | |
} | |
public void pushAll(Stack c) { | |
Stack tmp = new Stack(c); | |
while (!tmp.isEmpty()) { | |
push(tmp.pop()); | |
} | |
} | |
public void popAll(Stack c) { | |
while (!isEmpty()) { | |
c.push(pop()); | |
} | |
} | |
public boolean isEmpty() { | |
// TODO: implement | |
throw new UnsupportedOperationException("isEmpty"); | |
} | |
public static Object max(Stack s1) { | |
// TODO: implement | |
throw new UnsupportedOperationException("max"); | |
} | |
public static int numberOfElementsInCommon(Stack s1, Stack s2) { | |
// TODO: implement | |
throw new UnsupportedOperationException("isEmpty"); | |
} | |
// TODO: add a possibility to filter stack using predicates | |
private void ensureCapacity() { | |
if (size == elements.length) { | |
elements = Arrays.copyOf(elements, elements.length * 2 + 1); | |
} | |
} | |
} | |
package generics; | |
import static org.junit.Assert.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.NoSuchElementException; | |
import org.junit.Test; | |
public class StackTest { | |
@Test | |
public void testPushPop() throws Exception { | |
Stack s = new Stack(); | |
s.push("A"); | |
s.push("B"); | |
s.push("C"); | |
assertEquals("C", s.pop()); | |
assertEquals("B", s.pop()); | |
assertEquals("A", s.pop()); | |
try { | |
s.pop(); | |
fail(); | |
} catch (NoSuchElementException e) { | |
} | |
} | |
@Test | |
public void testCopyConstructor() throws Exception { | |
Stack s1 = new Stack(); | |
s1.push("A"); | |
s1.push("B"); | |
s1.push("C"); | |
Stack s2 = new Stack(s1); | |
assertEquals("C", s2.pop()); | |
assertEquals("B", s2.pop()); | |
assertEquals("C", s2.pop()); | |
assertTrue(s2.isEmpty()); | |
} | |
@Test | |
public void testIsEmpty() throws Exception { | |
Stack s = new Stack(); | |
assertTrue(s.isEmpty()); | |
s.push("A"); | |
assertFalse(s.isEmpty()); | |
s.pop(); | |
assertTrue(s.isEmpty()); | |
} | |
@Test | |
public void testPushAll() throws Exception { | |
List l = new ArrayList(); | |
l.add("A"); | |
l.add("B"); | |
l.add("C"); | |
Stack s = new Stack(); | |
s.pushAll(l); | |
assertEquals("C", s.pop()); | |
assertEquals("B", s.pop()); | |
assertEquals("A", s.pop()); | |
assertTrue(s.isEmpty()); | |
} | |
@Test | |
public void testPopAll() throws Exception { | |
List l = new ArrayList(); | |
Stack s = new Stack(); | |
s.push("A"); | |
s.push("B"); | |
s.push("C"); | |
s.popAll(l); | |
assertEquals("C", l.get(0)); | |
assertEquals("B", l.get(1)); | |
assertEquals("A", l.get(2)); | |
assertTrue(l.isEmpty()); | |
} | |
@Test | |
public void testMax() throws Exception { | |
throw new UnsupportedOperationException(); | |
} | |
@Test | |
public void testNumberOfElementsInCommon() throws Exception { | |
throw new UnsupportedOperationException(); | |
} | |
@Test | |
public void testFilter() throws Exception { | |
throw new UnsupportedOperationException(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment