This discussion is archived
4 Replies Latest reply: Mar 5, 2013 3:42 PM by 992891 RSS

Stack ADT using array

992891 Newbie
Currently Being Moderated
Hi everyone, I posted a similar question earlier and thought I got the response I wanted but I keep running into problems.

I'm looking to make my own version of the stack ADT using arrays (or arraylist).

I have created my own isEmpty() method. Here is part of my code:
public interface StackADT<T> {
     
     void push(T element); // adds an element to the stack
     T pop(); // removes an element from the stack and returns it
     boolean isEmpty(); // returns true if the stack is empty and false otherwise
     T peek(); // returns top element from the stack without removing it
     void truncate(); // truncates the stack to the exact number of elements
     void setExpansionRule(char rule); // sets expansion to either doubling or increasing by 10 elements

}
public class Stack<T> implements StackADT<T> {

          private T[] array;
          int index = 0;

          public Stack(){
          
               this.array = (T[]) new Object[50];
          }

          public boolean isEmpty(){
          
          if (index == 0){
               return true;
          }
          return false;
       }

           public T pop(){

                if(Stack<T>.isEmpty()){  //ERROR
                 throw new EmptyStackException("Stack is empty");     
                }
            }
}
I'm trying to use my isEmpty() method (also part of the stack class) inside other methods like pop(). Problem is it says: "T cannot be resolved to a variable, Stack cannot be resolved to a variable". I also tried Stack.isEmpty(), but it doesn't work. I'm really confused. Any suggestions?

Edited by: Tiberiu on Mar 1, 2013 6:38 PM
  • 1. Re: Stack ADT using array
    rp0428 Guru
    Currently Being Moderated
    >
    Hi everyone, I posted a similar question earlier and thought I got the response I wanted but I keep running into problems.

    I'm looking to make my own version of the stack ADT using arrays (or arraylist).

    I have created my own isEmpty() method. Here is part of my code:
    >
    No - you haven't. There is no 'isEmpty' method in the code you posted.

    We can't debug code that we can't see. You haven't posted any interface and you haven't posted anything that calls any of the limited code that you did post.

    You did post code that tries to call the method on the class instead of an instance of the class.
    if(Stack<T>.isEmpty()){  
  • 2. Re: Stack ADT using array
    992891 Newbie
    Currently Being Moderated
    Sorry, I'll edit it with the isEmpty() method inside. (I have an index at the beginning and I keep track of it with every push/pop operation) Also, about the code that tries to call the class itself, and not an instance, should I make an instance of the class inside the class itself at the beginning? Can I do that?

    Actually I think I got it, is the answer simply to write if (isEmpty())? without any class preceding it?

    Edited by: Tiberiu on Mar 1, 2013 6:37 PM
  • 3. Re: Stack ADT using array
    Kayaman Guru
    Currently Being Moderated
    Tiberiu wrote:
    Actually I think I got it, is the answer simply to write if (isEmpty())? without any class preceding it?
    Does it work?
  • 4. Re: Stack ADT using array
    992891 Newbie
    Currently Being Moderated
    Yes! I was trying to overcomplicate things before but it makes perfect sense

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points