Click to See Complete Forum and Search --> : Array stack generic class, non-static/static compile error


midni6htf4iry
January 24th, 2010, 04:57 PM
Ok so my assignment is to make an ArrayStack generic class, without a separate interface, and also a TestArrayStack with a "static main method" to test functionality with a sequence of integers. I have never worked with generic class until this moment and have never learned anything about it. Also, I have only been studying stacks/data structures for a week and so far I am terrible! I (think) that I created the generic class with no problems, here it is:


public class ArrayStack<T> {
private final int DEFAULT_CAPACITY = 500;
private T[] stack;
private int top;

public ArrayStack() {
top = 0;
stack = (T[])(new Object[DEFAULT_CAPACITY]);
}

public void push(T element) {
stack[top] = element;
top++;
}

public T pop() {
top--;
T result = stack[top];
stack[top] = null;
return result;
}

public boolean isEmpty(){
return top == -1;
}

public int size() {
return top+1;
}

}

class EmptyCollectionException extends Exception {

public EmptyCollectionException(String message)
{
super(message);
}

}



However, when I try to use ArrayStack.pop(); and ArrayStack.push(); in my static main method, obviously I get the error "non-static method cannot be referenced from a static context". How do I get around this?? Also, did I create my generic class for an array stack correctly? Thank you!

midni6htf4iry
January 24th, 2010, 06:25 PM
Ok nevermind I figured out why I cannot do ArrayStack.pop(); because I have to do

ArrayStack<Integer> stack = new ArrayStack<Integer>();
stack.pop();


However, I am having problems with my exception..

Here is my try/catch (which I am required to use)

try {
Object element = stack.pop();

}
catch (EmptyCollectionException exception)
{
System.out.println("Stack was empty!");
}
}

Is this correct? In addition to the exception class posted above? It tells me that my try statement does not throw the exception (I assume this is ok for now.. but im not sure if thats a problem)

dlorde
January 25th, 2010, 03:52 AM
You get the warning that the exception isn't thrown in your 'try' block because nothing in that 'try' block throws the exception. If you want to catch an exception that is thrown when you try to pop an empty stack, you should throw it from the pop method when it is called with an empty stack.

Generally, when you provide a custom exception, it makes sense to use it to hold information about the problem that caused it. The Exception class can hold a message for this purpose. In this case, why not use that to hold your informative message, rather than making the caller generate a message?class EmptyCollectionException extends Exception {

public EmptyCollectionException() {
super("The stack was empty!");
}
}
...
catch (EmptyCollectionException exception) {
System.out.println(exception.getMessage());
}Doing it this way means that the calling code doesn't have to guess what the exact problem was - the full details are in the exception message.

Oh, and the point of having a generic Stack (or any other collection class) is that you no longer need to get anonymous Objects out of it. If you insert a Foo, you can pop a Foo rather than popping an Object and casting to Foo.

Programs must be written for people to read, and only incidentally for machines to execute...
H. Abelson and G. Sussman