Results 1 to 7 of 7
  1. #1
    Rom 88 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Question Stack ADT using array

    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:

    Java 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
    
    }
    Java Code:
    public class Stack<T> implements StackADT<T> {
     
              private T[] array;
     
              public Stack(){
    		
                   this.array = (T[]) new Object[50];
              }
    
               public boolean isEmpty(){
    		
    		for(T elem: array){
    			if(elem != null){
    				return false;
    			}
    		}
    		return true;
    	}
     
               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?
    Last edited by Rom 88; 03-02-2013 at 03:39 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Stack ADT using array

    There are a number of potential problems here. I assume you are implementing your own stack backed up by an array.


    You're trying to use the class name of Stack to reference a non-existent method isEmpty(). And your doing it in a static way so you would not be able to access and instance of the array. What you really want to do is define an isEmpty() method that checks to see if the array has any elements in it. This is usually done maintaining an index to the current element in the array and adjusting it during push and pop operations.

    Also, you can't use Generic type parameters (T in your case) during execution as they are erased during class generation. Generic types and the type parameters are only meaningful to the compiler. You might also want to include your interface declaration of StackADT.

    Regards,
    Jim
    Last edited by jim829; 03-02-2013 at 02:48 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Rom 88 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stack ADT using array

    Sorry, I guess I should've posted my entire code, I did make an isEmpty() method, I just didn't paste it here. I'll edit the code. I'm not sure what you mean about the generics though...I was asked to implement the stack with generics using either an array or arraylist (but controlling all its operations). I read around and saw that in order to use an array with generics, I have to first make an array of objects and then cast it to T[] when inserting elements into the array.

    And I just understood what you meant with having an index to keep the locations with the push/pop operations. I'll definitely be implementing that instead of my isEmpty method. But for the time being, how am I supposed to even call the isEmpty method..? Do I make an instance of the class stack inside its own class?
    Last edited by Rom 88; 03-02-2013 at 02:59 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Stack ADT using array

    Check out The Java™ Tutorials. They have a lot of good information there. And within the last year they greatly improved the section on generics.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Rom 88 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stack ADT using array

    Just re-read and noticed you asked to show my implementation of stackADT, so I re-edited...and my problem isn't really generics (although I'm sure my code/knowledge on generics isn't perfect), but it's really how to be able to call the isEmpty method from my other methods. Do I make an instance of the stack class inside itself?

  6. #6
    Rom 88 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stack ADT using array

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

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Stack ADT using array

    Not quite. You don't need to provide the isEmpty() method so someone who creates a stack can do a test. It might look something like this if a user were using your class:
    Java Code:
    Stack stk = new Stack(10);  // stack size of 10.
    ..
    
    stk.push(1);
    stk.push(2);
    if (stk.isEmpty() == false) {
        int a = stk.pop();
       // do something with a;
    }
    This assumes the stack holds ints. However, internally to your Stack class the isEmpty method would check where the current element is in the array and return true or false if the stack is empty or not.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 01-14-2012, 08:56 PM
  2. Implementing a stack as an array
    By fam2315 in forum New To Java
    Replies: 2
    Last Post: 06-22-2011, 03:55 PM
  3. trouble creating stack using array
    By shashankc in forum New To Java
    Replies: 5
    Last Post: 01-20-2011, 12:49 PM
  4. Replies: 1
    Last Post: 03-25-2008, 05:06 AM
  5. how to convert a Java array to a java stack?
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-13-2007, 02:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •