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 to enter in parameters for it like my exception for the push method does. Therefore, when I put it in the test class and my stack is empty, it doesn't check the exception. This part of my class looks like this:
[code=java]
public Object peek() // Returns object on top of this stack without removing it from the stack. Throws an EmptyStackExcep tion if this stack is empty
{
if( top == -1 )
{
throw new EmptyStackExcep tion();
}
return stk[top];
}
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--];
}
[/code]
And in the test class I put..
[code=java] try
{
System.out.prin tln(newStack.pu sh("tart"));
System.out.prin tln(newStack.pu sh("chocolate") );
System.out.prin tln(newStack.po p());
System.out.prin tln(newStack.po p());
System.out.prin tln(newStack.po p());
}
catch(EmptyStac kException emptyEmpty)
{
System.out.prin tln("Stack is empty - could not carry out operation");
}
[/code]
When complied, the output reads
tart
chocolate
chocolate
tart
null
Could somebody please tell me if they see something wrong in my coding, and perhaps lead me to a solution? Thank you so much!
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 to enter in parameters for it like my exception for the push method does. Therefore, when I put it in the test class and my stack is empty, it doesn't check the exception. This part of my class looks like this:
[code=java]
public Object peek() // Returns object on top of this stack without removing it from the stack. Throws an EmptyStackExcep tion if this stack is empty
{
if( top == -1 )
{
throw new EmptyStackExcep tion();
}
return stk[top];
}
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--];
}
[/code]
And in the test class I put..
[code=java] try
{
System.out.prin tln(newStack.pu sh("tart"));
System.out.prin tln(newStack.pu sh("chocolate") );
System.out.prin tln(newStack.po p());
System.out.prin tln(newStack.po p());
System.out.prin tln(newStack.po p());
}
catch(EmptyStac kException emptyEmpty)
{
System.out.prin tln("Stack is empty - could not carry out operation");
}
[/code]
When complied, the output reads
tart
chocolate
chocolate
tart
null
Could somebody please tell me if they see something wrong in my coding, and perhaps lead me to a solution? Thank you so much!
Comment