Results 1 to 5 of 5

Thread: No output shown

  1. #1
    ai_2007 is offline Member
    Join Date
    Jun 2007
    Posts
    21
    Rep Power
    0

    Default No output shown

    I have created a code to accept 10 integers into a stack and a queue and then print the output simultaneously removing items from both.

    I can enter the inputs for both stack and queue, but there is no output shown.

    Why is this and how can I resolve it?

    Below is the code :

    Java Code:
    import javax.swing.*;
    import static javax.swing.JOptionPane.*;
    
    public class StacknQueue {
        public static void main(String args[]){
            StackArrayBased stack = new StackArrayBased();
            QueueArrayBased queue = new QueueArrayBased();
        
        
        for(int i=0; i<11; i++){
            stack.push(showInputDialog("Enter an integer into Stack :"));
            queue.enqueue(showInputDialog("Enter an integer into Queue : "));
        }
        
       System.out.println("\n Numbers stored in Stack \n");
       while((!stack.isEmpty())){
           System.out.println((String)stack.pop());
       }
       System.out.println("\n Numbers stored in Queue \n");
       while((!queue.isEmpty())){
           System.out.println((String)queue.dequeue());
       }
        
    }
    }

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    Tell us implementation details of "StackArrayBased", "QueueArrayBased" and "showInputDialog". It is impossible to give you the answer by looking at this code.

  3. #3
    ai_2007 is offline Member
    Join Date
    Jun 2007
    Posts
    21
    Rep Power
    0

    Default StackArrayBased and QueueArrayBased class

    These are the codes for StackArrayBased and QueueArrayBased classes. Together with these are StackInterface, StackException, QueueInterface and QueueException. All these classes has to be compiled besides StackArrayBased and QueueArrayBased.

    Java Code:
    /*
     * StackArrayBased.java
     *
     * Created on February 13, 2007, 1:05 AM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    /**
     *
     * @author AIGINI
     */
    public class StackArrayBased implements StackInterface
    {
        final int MAX_STACK = 50;           // max. size of stack
        private Object items[];
        private int top;
    
        public StackArrayBased()
        {
            items = new Object[MAX_STACK];
            top = -1;
        }
    
        public boolean isEmpty()
        {
            return top < 0;
        }
    
        public boolean isFull()
        {
            return top == MAX_STACK - 1;
        }
    
        public void push (Object newItem) throws StackException
        {
        if ( !isFull() )
            items[++top] = newItem;
        else
            throw new StackException ("Stk.Expn => " + " push : stack full");
        }
        public void popAll()
        {
            items = new Object[MAX_STACK];
            top = -1;
        }
    
        public Object pop() throws StackException
        {
            if ( !isEmpty() )
                return items[top--];
            else
                throw new StackException ("Stk.Excpn. ->> " + " pop: stack empty");
        }
    
        public Object peek() throws StackException
        {
            if ( !isEmpty() )
                return items[top];
            else
                throw new StackException ("Stk.exn. -=> " + " peek - stack empty");
        }
    }
    Java Code:
    /*
     * StackInterface.java
     *
     * Created on February 13, 2007, 1:06 AM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    /**
     *
     * @author AIGINI
     */
    public interface StackInterface {
            public boolean isEmpty();
    	//  Determine whether the stack is empty; 
    	//  Returns true if the stack is empty, otherwise returns false.
    
    	public boolean isFull();
    	//  Determine whether the stack is full; 
    	//  Returns true if the stack is full, otherwise returns false.
    
    	public void popAll();
    	//  Removes all the items from the stack.
    
    	public void push(Object newItem) throws StackException;
    	//  Adds an item to the top of a stack.
    	//  Exception	: Some implementations may throw StackException //  when newItem cannot be placed on the stack.
    
    	public Object pop() throws StackException;
    	//  Removes the top of a stack.
    	//  Exception	: Throws StackException if the stack is empty.
    
    	public Object peek() throws StackException;
    	//  Retrieves the top of a stack.
    	//  Exception	: Throws StackException if the stack is empty.
    }
    Java Code:
    /*
     * StackException.java
     *
     * Created on February 13, 2007, 1:06 AM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    /*
     *
     * @author AIGINI
     */
    public class StackException extends java.lang.RuntimeException 
    {
    	public StackException (String A) {
    		super(A);
    	}
    }
    /
    Java Code:
    *
     * QueueArrayBased.java
     *
     * Created on February 13, 2007, 12:48 AM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    /**
     *
     * @author AIGINI
     */
    public class QueueArrayBased  {  
    
    	private final int MAX_QUEUE = 5; // maximum size of queue
      
    	private Object[] items; 
    	private int front, back, count;  
    	
    	public QueueArrayBased() {    
    
    		items = new Object[MAX_QUEUE];    
    		front = 0;    
    		back = MAX_QUEUE-1;     
    		count = 0;
      	}  // end default constructor  // queue operations: 
    
    public boolean isEmpty() {    
    
    	return count == 0;  
    
    }  // end isEmpty  
    
    public boolean isFull() {    
    
    	return count == MAX_QUEUE;  
    }  // end isFull  
    
    public void enqueue(Object newItem) {    
    
    	if (!isFull()) {      
    
    		back = (back+1) % (MAX_QUEUE);      
    
    		items[back] = newItem;      
    
    		++count;    }    
    
    	else {     
    
    		throw new QueueException("QueueException on enqueue: "  + "Queue full");    
    
    	}  // end if 
    
    }  // end enqueue  
    
    public Object dequeue() throws QueueException {    
    
    	if (!isEmpty()) {      // queue is not empty; 
    
    		//remove front     
    
    		Object queueFront = items[front];      
    		front = (front+1) % (MAX_QUEUE);      
    
    		--count;      
    		return queueFront;    
    
    }    
    
    	else {      
    
    		throw new QueueException("QueueException on dequeue: "+  "Queue empty");    
    
    	}   // end if  
    
    }  // end dequeue  
    
    public void dequeueAll() {    
    
    	items = new Object[MAX_QUEUE];      
    
    	front = 0;    
    	back = MAX_QUEUE-1;    
    	count = 0;  
    }  // end dequeueAll  
    
    public Object peek() throws QueueException {    
    
    	if (!isEmpty()) {       // queue is not empty; 
    	//retrieve front      
    	return items[front];   
    }
    else {      
        throw new QueueException("Queue exception on peek: " + "Queue empty");
    }   // end if  
    
    }  // end peek
    
    
    } // end QueueArrayBased
    Java Code:
    /*
     * QueueInterface.java
     *
     * Created on February 13, 2007, 12:58 AM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    /**
     *
     * @author AIGINI
     */
    // QueueInterface.java
    // interface for the Queue ADT
    
    public interface QueueInterface{
    
       // isEmpty
       // pre: none
       // post: returns true if this Queue is empty, false otherwise
       public boolean isEmpty();
    
       // length
       // pre: none
       // post: returns the length of this Queue.
       public int length();
    
       // enqueue
       // adds newItem to back of this Queue
       // pre: none
       // post: !isEmpty()
       public void enqueue(Object newItem);
    
       // dequeue
       // deletes and returns item from front of this Queue
       // pre: !isEmpty()
       // post: this Queue will have one fewer element
       public Object dequeue() throws QueueException;
    
       // peek
       // pre: !isEmpty()
       // post: returns item at front of Queue
       public Object peek() throws QueueException;
    
       // dequeueAll
       // sets this Queue to the empty state
       // pre: !isEmpty()
       // post: isEmpty()
       public void dequeueAll() throws QueueException;
    
       // toString
       // overrides Object's toString() method
       public String toString();
    }
    Java Code:
    /*
     * QueueException.java
     *
     * Created on February 13, 2007, 12:57 AM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    /**
     *
     * @author AIGINI
     */
    public class QueueException extends java.lang.RuntimeException {
        public QueueException(String s) {
            super(s);
        }
    }

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    What about showInputDialog method? We miss it too.. ;)

  5. #5
    ai_2007 is offline Member
    Join Date
    Jun 2007
    Posts
    21
    Rep Power
    0

    Default showInputDialog

    The showInputDialog is to accept input from the user. But since the import statement :

    import static javax.swing.JOptionPane.*;


    has already been declared, so there is no need to enter JOptionPane as always done.

Similar Threads

  1. Writing an XML output
    By JThangiah in forum XML
    Replies: 2
    Last Post: 03-27-2008, 04:15 PM
  2. Output to excel
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 01:19 AM
  3. output
    By Camden in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 10:34 PM
  4. How to redirect the output
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:30 PM
  5. get the output from whoami
    By gary in forum Advanced Java
    Replies: 2
    Last Post: 06-12-2007, 01:05 PM

Posting Permissions

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