Hallo, ich bin dabei einen Stack zu implementieren, welcher einen JUnit4 Test erfüllen sollte.
Ich habe eigentlich alles fertig, jedoch wird der Test nicht erfüllt. Ich sehe einfach den Fehler nicht ein, vielleicht kann mir da jemand helfen:
Ich habe eigentlich alles fertig, jedoch wird der Test nicht erfüllt. Ich sehe einfach den Fehler nicht ein, vielleicht kann mir da jemand helfen:
Java:
package u4a1;
import java.util.EmptyStackException;
/**
* Dynamically growing stack.
*/
public class Stack {
private int[] buffer;
private int size;
/**
* Creates a new stack
*
* @param capacity the initial capacity of the stack
*/
public Stack(int capacity) //Constructor
{
size = 0;
buffer = new int [capacity];
}
/**
* Converts stack to a string representation.
*
* @return A ", "-separated list of the numbers, enclosed in square brackets. Bottom numbers come first.
*/
public String toString() //convert Stack toString
{
String stackString = "";
for (int i = 0; i<size;i++)
{
stackString = stackString + buffer[i] + ", ";
}
return stackString;
}
/**
* Doubles the capacity of the stack.
*
* Copies all objects to a new buffer of doubled size.
*/
private void grow()
{
int [] copyBuffer = new int [2*buffer.length];
for (int i = 0; i<size;i++)
{
copyBuffer[i] = buffer[i];
}
buffer = copyBuffer;
}
/**
* Pushes a number onto the top of this stack.
*
* Grows the stack if necessary.
*
* @param number the number to be pushed onto this stack.
*/
public void push(int number)
{
if (size >= buffer.length)
{
grow();
size = size + 1;
buffer[size] = number;
}
else
{
size = size + 1;
buffer[size] = number;
}
}
/**
* Removes the number at the top of this stack and returns it as the value of this function.
*
* @return The number at the top of this stack
* @throws EmptyStackException if this stack is empty
*/
public int pop() throws EmptyStackException
{
int topNumber;
if (empty())
{
topNumber = buffer[size];
size--;
}
else
{
throw new EmptyStackException();
}
return topNumber;
}
/**
* Looks at the number at the top of this stack without removing it from the stack.
*
* @return the number at the top of this stack
* @throws EmptyStackException if this stack is empty
*/
public int peek() throws EmptyStackException
{
int topNumber;
if (empty())
{
topNumber = buffer[size];
}
else
{
throw new EmptyStackException();
}
return topNumber;
}
/**
* Tests if this stack is empty.
*
* @return true if and only if this stack contains no items; false otherwise.
*/
public boolean empty()
{
if (size == 0)
{
return true;
}
else
{
return false;
}
}
/**
* Get the size of this stack.
*
* @return the current number of items on this stack
*/
public int size()
{
return size;
}
/**
* Get the capacity of this stack.
*
* @return the maximum number of items this stack can hold without having to grow
*/
public int capacity()
{
return buffer.length;
}
}