Image

Imagekritof wrote in Imagejava_dev

Newbie needs JAVA HELP!

Hi! I'm very new to Java, and I'm having some trouble running this simple program, which is supposed to test run a stack. Can anyone give me some pointers? I've managed to eliminate most of the errors, and there just seems to be something wrong with the last portion below the words "//Tests all instances of objArrayStack".

Any and all help will be GREATLY appreciated!!!! Thanks in advance!
(Sorry for any x-posting.)



//Text of the Stack interface
//"Menu" or list of things that can be done to any Stack regardless
//of implementation
public interface Stack
{
	//Tests whether current stack is empty
	//Returns true if so, false if not
	public boolean isEmpty();

	//Retrieves value at the top of the current stack
	//Precondition: Current stack is not empty
	public Object top() throws StackException;

	//Push method: pushes an Object value on the top of the
	//current stack
	public void push (Object value) throws StackException;
}//Terminates code of Stack interface

//NOTE: StackException names a programmer-defined exception class,
//that will return a message to the user whenever a stack overflow or
//underflow experienced

//Text of the StackException class:
public class StackException extends RuntimeException
{
	//constructor
	public StackException()
	{
		super();
	}//terminates text of constructor

}//terminates text of StackException class

//Text of the implementation class for Stack, using arrays:
public class objArrayStack implements Stack
{
	//implementation class for Stack, using arrays
	//constructor: initiates stack as empty
	public objArrayStack()
	{
		topValue=-1;
	}

	//Tests whether current stack is empty
	public boolean isEmpty()
	{
		return topValue==-1;
	}//terminates text of isEmpty

	//push method
	public void push(Object value) throws StackException
	{
		if (topValue < ArraySize-1) //Current stack is not full
						 		    //Legitimate push operation
		Info[++topValue] = value;
		else //Current stack is full: Signal overflow.
		throw new StackException("Error: Overflow.");
	}//terminates text of push

	//pop method
	public void pop() throws StackException
	{
		if(!isEmpty()) //Current stack is not empty
					   //Legitimate pop operation
			--topValue;
		else //Current stack is empty: Signal underflow.
		throw new StackException("Error: Underflow.");
	}//terminates pop method

	//top method
	public Object top() throws StackException
	{
		if(!isEmpty) //Legitimate top
		return Info[topValue];
		else //underflow
		throw new StackException ("Underflow: Top operation aborted");
	}//terminates top method

	final int ArraySize = 10;
	private Object Info [] = new Object [ArraySize];
	private int topValue;
}//terminates text of objArrayStack implementation class


//Tests all instances of objArrayStack
public static void main (String []args)
{
	//Construct the generic Stack object
	objArrayStack instStack = new objArrayStack();

	//Push several integer objects onto intStack
	for (int i=0; i<=6; ++i)
	{
		intStack.push(new Integer(i));
		System.out.println(Integer(i) +" is pushed onto intStack.");
	}

	while(!intStack.isEmpty())
	{
		Integer value = (Integer) intStack.top();
		System.out.println(value + " is popped from intStack.");
		intStack.pop();
	}

	if(intStack.isEmpty())
	System.out.println("intStack is currently empty.");
	else
	System.out.println("intStack is not currently empty.");
}//terminates the text of the main method.