Results 1 to 7 of 7

Thread: Queue

  1. #1
    DCY
    DCY is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Queue

    Hello..I am try to implement the queue below so that it prints out the smallest number but so far it is only printing out the first number in the queue. Thank you

    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class Assign1 {
        private final int MAX_QUEUE_SIZE = 5;
        private final int NO_SIMULATIONS_TO_RUN = 7; 
        
        private int maxSize;
    
      private double[] queArray;
    
      private int nItems;
    
          /**
         * main method
         * @param args
         */
        public static void main( String args[] ) {
           new Assign1().runSimulation();
        }
        
        /**
         * run a simulation priority queue
         */
        public void runSimulation() {
            
            PriorityQueue queue = new PriorityQueue();
            initQueue(queue);
            
            for (int i = 0; i < NO_SIMULATIONS_TO_RUN; i++) {
                
                System.out.println( "The numbers in the queue are: " + queue.toString());
        
                System.out.println( "The current dispatched number is " + queue.poll());
                queue.remove();
                int item = getRandomNumber();
                
                System.out.println( "The new coming number is " + item );
                queue.add(item);   
                System.out.print( "\n\n" );
            }
        }
        /**
         * creates and inits a queue
         * @param queue
         */
        private void initQueue(PriorityQueue queue){
            for (int i = 0; i < MAX_QUEUE_SIZE; i++) 
                queue.add(getRandomNumber());
        }
        /**
         * returns a RandomNumber()
         * @return
         */
        public static int getRandomNumber(){
            return 1 + ( int ) ( Math.random() * 100 );
        }
        
        /**
         * custom queue class
         */
        private class PriorityQueue extends ArrayList {
    
            /**
             * allows us to add int values into an ArrayList
             * @param val
             */
            public void add(int val){
                super.add(new Integer(val));
            }
            /**
             * poll for the current top item in the list 
             * @return
             */
            public int poll(){
               return ((Integer) iterator().next()).intValue(); 
            }
            /**
             * returns the items in queue out
             * @param queue
             */
            public String toString(){
                StringBuffer buff = new StringBuffer();
                Iterator it = iterator();
                buff.append("[");
                while (it.hasNext()) {
                    buff.append(it.next());
                    if (it.hasNext())
                        buff.append(", ");
                }
                buff.append("]");
                return buff.toString();
            }
            /**
             * remove the top level item if the list is not empty
             */
            public void remove(){
                if (!isEmpty())
                    remove(0);
            }
        }
    }

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    the first number in the priority queue should be indeed the smallest one, so your poll() is correct. When you add things to the queue though, you should make sure to insert them in the right place rather than in the end of the queue.

  3. #3
    DCY
    DCY is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Yeh but when I run the program.. Instead of giving me the smallest number it just gives me the first number in the queue..but this is not the smallest.

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    well makes sense. you're currently adding numbers to the end of your queue. What you should do instead is add them in the right place of the queue. So instead of
    Java Code:
    public void add(int val){
       super.add(new Integer(val));
    }
    you should have something like

    Java Code:
    public void add(int val){
       int index = calculateTheRightIndex(val);
       super.add(index, new Integer(val));
    }

  5. #5
    DCY
    DCY is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    I tried it but its giving me an error on this line

    Java Code:
    int index = calculateTheRightIndex(val);
    it says cannot fine method calculateTheRightIndex

  6. #6
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    right. i was trying to give you a direction, not to write your code, see

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Your PriorityQueue is just an ArrayList, what more do you want? Naming it a PriorityQueue doesn't make it a real PriorityQueue. b.t.w. a real PriorityQueue already exists; why reinvent the wheel?

    kind regards,

    Jos

Similar Threads

  1. Queue in the GUI
    By jonywalker123 in forum Advanced Java
    Replies: 2
    Last Post: 03-06-2009, 08:33 PM
  2. a Queue of customer in the GUI
    By jonywalker123 in forum Advanced Java
    Replies: 2
    Last Post: 02-26-2009, 11:02 PM
  3. Declaring a Queue
    By rhm54 in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 05:02 AM
  4. Help with queue array please!!
    By vmcg105 in forum New To Java
    Replies: 1
    Last Post: 03-08-2008, 07:03 PM
  5. Using a queue
    By Krmeus in forum New To Java
    Replies: 0
    Last Post: 12-10-2007, 03:38 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
  •