Results 1 to 7 of 7
  1. #1
    Zompire is offline Member
    Join Date
    Jul 2016
    Posts
    8
    Rep Power
    0

    Default linked stack when print does read desired output

    here is some stock code
    Java Code:
    public interface StackADT<T>
    {
        /**  
         * Adds the specified element to the top of this stack. 
         * @param element element to be pushed onto the stack
         */
        public void push(T element);
      
        /**  
         * Removes and returns the top element from this stack. 
         * @return the element removed from the stack
         */
        public T pop();
    
        /**  
         * Returns without removing the top element of this stack. 
         * @return the element on top of the stack
         */
        public T peek();
      
        /**  
         * Returns true if this stack contains no elements. 
         * @return true if the stack is empty
         */
        public boolean isEmpty();
    
        /** 
         * Returns the number of elements in this stack. 
         * @return the number of elements in the stack
         */
        public int size();
    
        /**  
         * Returns a string representation of this stack. 
         * @return a string representation of the stack
         */
        public String toString();
    }
    Java Code:
    public class EmptyCollectionException extends RuntimeException
    {
        /**
         * Sets up this exception with an appropriate message.
         * @param collection the name of the collection
         */
        public EmptyCollectionException(String collection)
        {
            super("The " + collection + " is empty.");
        }
    }
    Java Code:
    public class LinearNode<T>
    {
        private LinearNode<T> next;
        private T element;
     
        /**
         * Creates an empty node.
         */
        public LinearNode()
        {
            next = null;
            element = null;
        }
     
        /**
         * Creates a node storing the specified element.
         * @param elem element to be stored
         */
        public LinearNode(T elem)
        {
            next = null;
            element = elem;
        }
     
        /**
         * Returns the node that follows this one.
         * @return reference to next node
         */
        public LinearNode<T> getNext()
        {
            return next;
        }
     
        /**
         * Sets the node that follows this one.
         * @param node node to follow this one
         */
        public void setNext(LinearNode<T> node)
        {
            next = node;
        }
     
        /**
         * Returns the element stored in this node.
         * @return element stored at the node
         */
        public T getElement()
        {
            return element;
       }
    }
    here is my code
    Java Code:
    import java.util.Iterator;
    
    public class Turner_A06Q1
    {
        /**
         * Program entry point for stack testing.
         * @param args Argument list.
         */    
        public static void main(String[] args)
        {
            LinkedStack<Integer> stack = new LinkedStack<Integer>();
            
            System.out.println("STACK TESTING");
            
            System.out.println("The stack contains:\n" + stack.toString());
            
            stack.push(3);
            stack.push(7);
            stack.push(4);
            System.out.println(stack.peek());
            stack.pop();        
            stack.push(9);
            stack.push(8);
            System.out.println(stack.peek());        
            System.out.println(stack.pop());
            System.out.println(stack.peek());        
            
            System.out.println("The size of the stack is: " + stack.size());
            System.out.println("The stack contains:\n" + stack.toString());        
        }
        
        /**
         * Represents a linked implementation of a stack.
         *
         * @author Java Foundations 
         * @version 4.0
         */
        public static class LinkedStack<T> implements StackADT<T>
        {
            private int count;  
            private LinearNode<T> top; 
        
            /**
             * Creates an empty stack.
             */
            public LinkedStack()
            {
                count = 0;
                top = null;
            }
        
            /**
             * Adds the specified element to the top of this stack.
             * @param element element to be pushed on stack
             */
            public void push(T element)
            {
                LinearNode<T> temp = new LinearNode<T>(element);
        
                temp.setNext(top);
                top = temp;
                count++;
            }
        
            /**
             * Removes the element at the top of this stack and returns a
             * reference to it. 
             * @return element from top of stack
             * @throws EmptyCollectionException if the stack is empty
             */
            public T pop() throws EmptyCollectionException
            {
                if (isEmpty())
                    throw new EmptyCollectionException("stack");
        
                T result = top.getElement();
                top = top.getNext();
                count--;
         
                return result;
            }
           
            /**
             * Returns a reference to the element at the top of this stack.
             * The element is not removed from the stack.  
             * @return element on top of stack
             * @throws EmptyCollectionException if the stack is empty  
             */
            public T peek() throws EmptyCollectionException
            {
                return top.getElement();
            }
        
            /**
             * Returns true if this stack is empty and false otherwise. 
             * @return true if stack is empty
             */
            public boolean isEmpty()
            {
               return (top == null);
            }
         
            /**
             * Returns the number of elements in this stack.
             * @return number of elements in the stack
             */
            public int size()
            {
                return count;
            }
        
            /**
             * Returns a string representation of this stack. The string has the
             * form of each element printed on its own line, with the top most
             * element displayed first, and the bottom most element displayed last.
             * If the list is empty, returns the word "empty".
             * @return a string representation of the stack
             */
            public String toString()
                {
                if(isEmpty()) {
                    return "";
                }
               StringBuilder sb = new StringBuilder(top.toString());
                LinearNode<T> next = top.getNext();
                while(next != null) {
                    sb.append("\n").append(next);
                    next = next.getNext();
                }
                return sb.toString();
           }
            }
        }
    here is what i am getting
    STACK TESTING
    The stack contains:

    4
    8
    8
    9
    The size of the stack is: 3
    The stack contains:
    LinearNode@1a20c2e
    LinearNode@1a517dd
    LinearNode@1afdd83
    and here is what i should be getting
    STACK TESTING

    The stack contains:

    empty

    4

    8

    8

    9

    The size of the stack is: 3

    The stack contains:

    9

    7

    3

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: linked stack when print does read desired output

    Can you describe what is wrong with the output?

    Are you complaining about the Strings returned by the LinearNode's toString() method: LinearNode@1afdd83
    If you want to see something else there, override the class's toString() method and return the String you want to see.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Zompire is offline Member
    Join Date
    Jul 2016
    Posts
    8
    Rep Power
    0

    Default Re: linked stack when print does read desired output

    yes i dont understand exactly why its doing that please help

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: linked stack when print does read desired output

    Java Code:
    sb.append("\n").append(next);
    This line (127 in Turner_A06Q1 above) is appending the result of calling toString on the object referenced by 'next'.
    'next' is a LinearNode.
    Since LinearNode has not overridden the toString() method of Object then that is the method that will be used, which displays the class name '@' and the hashcode.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: linked stack when print does read desired output

    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Zompire is offline Member
    Join Date
    Jul 2016
    Posts
    8
    Rep Power
    0

    Default Re: linked stack when print does read desired output

    Quote Originally Posted by Tolls View Post
    Java Code:
    sb.append("\n").append(next);
    This line (127 in Turner_A06Q1 above) is appending the result of calling toString on the object referenced by 'next'.
    'next' is a LinearNode.
    Since LinearNode has not overridden the toString() method of Object then that is the method that will be used, which displays the class name '@' and the hashcode.
    Can someone give me an example of what you would write for the toString() method I am at a loss

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: linked stack when print does read desired output

    When you print out a linearNode object, what do you really want to print? That is what you should return as a string value.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 11-27-2014, 12:14 AM
  2. toString Method for a linked stack
    By pyler in forum New To Java
    Replies: 1
    Last Post: 10-03-2013, 04:48 AM
  3. Output to file not writing out desired string
    By alex.start in forum New To Java
    Replies: 11
    Last Post: 10-02-2013, 07:58 AM
  4. Replies: 1
    Last Post: 02-13-2012, 02:51 PM
  5. Stack Implemented Linked List HELP!!!!!!!!!
    By dudedatroz in forum NetBeans
    Replies: 1
    Last Post: 02-19-2010, 10:56 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
  •