I would like to know if this is a great stack implementation. I created my own exception. Was that a bad idea or unnecessary? Should I have just thrown a IndexOutOfBounds exception instead of my own.
import java.util.*;
public class Stack2{
private int[] stack;
private int size;
public Stack2(){
stack = new int[10];
size = 0;
}
public Stack2(int height){
if(height <= 0){
throw new EmptyStackException();
}
stack = new int[height];
size = 0;
}
public void add(int value){
if (size == stack.length){
throw new StackOverflowException();
}
stack[size] = value;
size++;
}
public int pop(){
if(size == 0){
throw new EmptyStackException();
}
size--;
return stack[size + 1];
}
public int peek(){
if(size == 0){
throw new EmptyStackException();
}
return stack[size - 1];
}
public boolean isEmpty(){
if(size == 0){
return true;
}
return false;
}
public int getSize(){
return this.size;
}
class StackOverflowException extends RuntimeException{
public StackOverflowException(){
super("Nothing can be added to the stack. The stack was full and has overflowed");
}
public StackOverflowException(String message){
super(message);
}
}
}
StackOverflowException
class that you never use? Would you like to post yourEmptyStackException
class? – 200_success♦ Jul 26 '16 at 18:01add(3);pop()
returns 0). As such I'm voting to close this question as off-topic because the code is not working as intended. For more information, please see the help center. Thanks – Vogel612 Jul 27 '16 at 7:47