User Profile
Collapse
-
[CODE=java]import java.util.*;
public class Stack
{
private Object stk[];
private int top;
public Stack()
{
stk= new Stack[50]; //creates a default setting of 50 objects in the stack.
top= -1; //sets top of stack to -1 to show there is no objects in the stack yet.
}
public Stack(int size)...Last edited by r035198x; Nov 5 '08, 07:28 AM. Reason: added code tags. Please don't forget them next time.Leave a comment:
-
Oh Jos, I understand what you are saying. The goal of the assignment though is to utilize a try/catch block for an exception that is thrown. So I am supposed to handle the EmptyStackExcep tion using the try/catch block. I just don't see how this is possible if the complier will not allow me to pass a message in the exception's parameters.Leave a comment:
-
r, my pop method is as follows.
public Object pop() // Removes and returns object at top of this stack. Throws an EmptyStackExcep tion if the stack is empty
{
if( top == -1 )
{
throw new EmptyStackExcep tion();
}
return stk[top--];
}
Jos,
are you implying that I should put the while loop in the try/catch block?
Thank you both!Leave a comment:
-
Exceptions
Hello all! I'm relatively new to Java, so I'm sorry if this question comes off as naive. I am creating an array-based stack class. The goal of it is to work on exceptions.
I have 2 classes, a test class and a regular class. The regular class has the methods object peek(), object push(), and object pop(). For the peek and the pop methods I am using an EmptyStackExcep tion. For some reason, the EmptyStackExcep tion will not allow me...
No activity results to display
Show More
Leave a comment: