0
\$\begingroup\$

Here is an array based stack. I tried to implement this basic stack from memory. I want to make sure it is correct.

import java.util.*;

public class Stack{
    private int[] stack;
    private int size;

    public Stack(){
        stack = new int[5];
        int size = 0;
    }

    public Stack(int capacity){
        stack = new int[capacity];
        int size = 0;
    }

    public void push(int pushedValue){
        if(size == stack.length){
            throw new fullStackException();
        }
        stack[size] = pushedValue;
        size++;
    }

    public int pop(){
        if(size == 0){
            throw new emptyStackException();
        }
        int poppedValue = stack[size - 1];
        size--;
        return poppedValue;
    }

    public int peek(){
        if(size == 0){
            throw new emptyStackException();
        }
        return stack[size - 1];
    }

    private class emptyStackException extends RuntimeException{
        public emptyStackException(){
            super();
        }
    }

    private class fullStackException extends RuntimeException{
        public fullStackException(){
            super();
        }
    }

}
\$\endgroup\$
1
  • \$\begingroup\$ Why don't you look up a proper implementation and compare it to yours? Figuring out why they did something that you didn't do will be a far more valuable experience. \$\endgroup\$ Commented Aug 16, 2016 at 13:14

2 Answers 2

2
\$\begingroup\$

Well, it seems to be ok, a simple unit test prove it works.

However, using such private exceptions is not a good idea because nobody can catch them. You should also use the CamelCase name for those exceptions and maybe provide more context information to those exceptions.

\$\endgroup\$
0
\$\begingroup\$

Right now your stack is a fixed size, which is fine, but you could make it expand and shrink like an ArrayList does if you were interested: double size when maximum capacity reached, halve size when quarter capacity reached.

Maybe something else to think about is adding a method that would return the current or maximum size if you think that would be useful; it's up to you. Something I would recommend, however, is implementing an isEmpty and maybe an isFull method. Having an isEmpty method would be very useful when popping everything off a stack rather than waiting for an emptyStackException. But if a user decides to ignore the return value from isEmpty or possibly isFull, then it's their own fault for invoking the exception.

I agree with @Anonymous about those exception classes being capitalized. Classes in Java should be TitleCase; methods, fields, and other variables in camelCase; and final fields and enums in UPPER_CASE. That's just some general Java variable naming conventions.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.