Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Basic stack help needed

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Basic stack help needed

    I'm trying to create my own stack class but am having difficulties and getting only null when i output...here's my code:

    public class A_Stack<T> implements StackInterface<T> {
     
    	     private T[] stack = null;
    	     private int top = 0;
     
    	     public A_Stack(int maxSize) {
     
    	           stack = (T[])(new Object[maxSize]);
     
    	     }
     
    		@Override
    		public boolean isEmpty() {
    			boolean result = false;
    			if(this.getSize() == 0){
    				result = true;
    			}
    			return result;
    		}
     
    		@Override
    		public String toString() {
    			String string = "top = " + top + "stack = ";
    			for (int i = 0; i < stack.length; i++){
    				string += stack[i].toString();
    			}
    			return string;
    		}
    		@Override
    		public boolean isFull() {
    			boolean result = false;
    			if(this.getSize() == stack.length) {
    			result = true;
    			}
    			return result;
    		}
     
    		@Override
    		public int getSize() {
    			int size = stack.length;
    			return size;
    		}
     
    		@Override
    		public T peek() {
    			T peek = stack[top];
    			return peek;
    		}
     
    		@Override
    		public T pop() {
    			if (top == 0){
    				throw new RuntimeException("STACK EMPTY");
    			}
    			T result = stack[top--];
    			stack[top] = null;
    			return result;
    		}
     
    		@Override
    		public void push(T o) {
    			if (top == stack.length) {
    				throw new RuntimeException("STACK FULL");
    			}
    			stack[top++] = o;
    		}   
     
    	}
     
    public class StackTest {
    	public static void main(String[] args) {
    		A_Stack<String> stack = new A_Stack<String>(10);
    		System.out.println("top of an empty stack: " + stack.peek());
    		stack.push("Joe");
    		stack.push("Mark");
    		stack.push("Jessica");
    		System.out.println("top of non-empty stack: " + stack.peek());
    		System.out.println("popped stack: " + stack.pop());
    		System.out.println("popped stack: " + stack.pop());
    		System.out.println("isFull: " + stack.isFull());
    		System.out.println("isEmpty: " + stack.isEmpty());
    		System.out.println("popped stack: " + stack.pop());
    		System.out.println("isEmpty: " + stack.isEmpty());
    		System.out.println("popped stack: " + stack.pop());
    		System.out.println("isEmpty: " + stack.isEmpty());
    		for(int i=0; i<10; i++) {
    			stack.push("" + i);
    		}
    		System.out.println(stack.toString());
    		// deliberately adding one too many objects to the stack
    		stack.push("" + 10);
    	}


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Basic stack help needed

    How can the posted code be tested? I don't see a main() method for execution.

    Also please copy the full text of the error messages and pasted it here.

Similar Threads

  1. Grails Developer Needed - URGENTLY NEEDED *WORK FROM HOME*
    By IngeniumR in forum Paid Java Projects
    Replies: 1
    Last Post: March 15th, 2013, 08:14 PM
  2. Basic 2D Game Help Needed
    By Kez323 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 13th, 2012, 02:37 PM
  3. Very basic help needed
    By goochasauras in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 26th, 2012, 11:50 PM
  4. stack
    By ridg18 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 11:45 AM
  5. Stack
    By AmyH in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 7th, 2010, 03:04 PM