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 7 of 7

Thread: Priority Queue using comparable

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Priority Queue using comparable

    Hey,

    Im trying to make a Prioity Queue that uses the queue interface and comparable.. The objects will be placed in depending on their priority.. i'm having a bit of trouble with it, probaly cause i'm quite new to Comparables..
    I have the following but am having trouble even understanding what it is my code is doing!!

    	public int compareTo(Object o) 
    	{
    		// 1:   films duration is greater than 'o'
    		if(this.duration > ((Film)o).duration)
    			return 1;
    		// 0:   films duration equals 'o'
    		else if(this.duration == ((Film)o).duration)
    			return 0;
    		// -1:   films durations is less than 'o'
    		return -1;
    	}

    public void enqueue (Comparable o) 
    	{
     
    		if(isEmpty())
    		{
    			int temp =rear;
    			rear= (rear + 1) % queue.length;
    			if (front ==rear) 
    			{
    				rear = temp;
    				throw new FullQueueException ();
    			}
    			queue[rear] = o;
    		}
     
     
    		else
    		{
     
    			for(int i = 0; i <= queue.length; i++)
    			{
    				//duration is greater so everything must be moved right
    				if(o.compareTo(queue[i]) == 1 )
    				{
    					for(int x = 0; x <= queue.length; x++)
    					{
    						int temp =rear;
    						rear= (rear + 1) % queue.length;
    						if (front ==rear) 
    						{
    							rear = temp;
    							throw new FullQueueException ();
    						}
    						queue[rear] = queue[rear+1];
    					}
    					queue[front] = o;
    				}
     
    				if(o.compareTo(queue[i]) == 0 )
    				{
     
     
    				}

    Any help appreciated


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Priority Queue using comparable

    I presume you are writing this yourself as an exercise or homework? Java provides the PriorityQueue class.

    What exactly is the problem you are having, as its not obvious (at least to me) from the posted code?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Priority Queue using comparable

    I take it that the variable duration is a number? You can just subtract the two numbers. Also, Comparable interfaces must implement what they are comparable to (generally you don't want this to be a wild-card or the base Object class)

    public class Film implements Comparable<Film>
    {
        // ... other code
     
        public int compareTo(Film o)
        {
            return this.duration - o.duration;
        }
    }

    public void enqueue (Comparable<Film> o)

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Priority Queue using comparable

    I think we have to make it generic and able to work with any kind of object?? Ye its a class exercise.. im just wondering if my enqueue method is on the right track.. "Joining the queue (enqueue)will involve finding the correct position for the new item, based on its priority, i.e. all the items before it in the queue should have >= priority; all items after it should have a lower (<) priority."

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Priority Queue using comparable

    I kind of doubt it, it makes no sense to try and compare say a BufferedImage object to your Film object.

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Priority Queue using comparable

    Ok, ill give it try.. Am i along the right track with what i have though??

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Priority Queue using comparable

    I'm having trouble trying to move all elements right in this queue. For example, I need to place something in between others because it's priority says it's in there, so how would i make space for it in my circular array queue?? thanks again, hope i'm making sense!

Similar Threads

  1. Implementing a queue or stack using a heap
    By tla280 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 30th, 2010, 11:29 PM
  2. Replies: 4
    Last Post: July 21st, 2010, 04:07 PM
  3. Comparable Interface
    By yelrubk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:08 AM
  4. How do I fix my program so I can use the keyboard to type in the numbers?
    By rocafella5007 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 29th, 2009, 02:39 PM