Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 49
  1. #21
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Compare your Person1 class with Berkerleybross' code: your Person1 class still doesn't "implements Comparable", although the method is there ...

    kind regards,

    Jos

  2. #22
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    That code works perfectly fine for me.
    Have you got the latest version of java etc?

    Please can you give us the exact error message produced when pq2 fails, including the full
    stack trace

    eg
    Java Code:
    run:
    pq2 is: [javaapplication14.Person1@9304b1]
    Exception in thread "main" java.lang.RuntimeException: wrong class
    Comparing
            at javaapplication14.Person1.compareTo(Main.java:55)
            at java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:582)
            at java.util.PriorityQueue.siftUp(PriorityQueue.java:574)
            at java.util.PriorityQueue.offer(PriorityQueue.java:274)
            at java.util.PriorityQueue.add(PriorityQueue.java:251)
            at javaapplication14.Main.main(Main.java:21)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    It would also help if you implemented the toString method. Since i gave it to you really have no excuse.

    How do you know if Person1@8a52b6 is dan or john?
    You may think its dan, but it *might* be john - you have no way of knowing. Until you use the toString method.

    I'm becoming a bit exasperated by your refusal to use the toString method so i refuse to help you any more until you do. You've got to help us to help you.

    By all means come back and ask for more help when you have implemented the toString method, and when you have a complete error message.

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

    Default

    Quote Originally Posted by berkeleybross View Post
    I'm becoming a bit exasperated [ ... ]
    Duh, you ain't seen nothing yet; you should read that 'html parsing' thread ;-)

    kind regards,

    Jos

  4. #24
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Yep.
    This on is barely on page 2...

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

    Default

    Quote Originally Posted by Tolls View Post
    Yep.
    This on is barely on page 2...
    It's still the first page for me (40 replies per page) but much more important is my private (not so new) theorem which is mine *ahem*: the probability of a succesfull ending of a thread is inversely proportional to the length of the thread. *ahem*, this was my theorem which is mine. As a corollary: brontosauruses were also very long and we all know how they ended ...

    kind regards,

    Jos ;-)

  6. #26
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Your knowledge of the greats is impeccable.

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

    Default

    Quote Originally Posted by Tolls View Post
    Your knowledge of the greats is impeccable.
    I know, do you want me to state my theorem (which is mine) again?

    kind regards,

    Jos (*ahem* ;-)

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    If it involves Proust then I don't want to know...

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

    Default

    Quote Originally Posted by Tolls View Post
    If it involves Proust then I don't want to know...
    No, there's no Proust in it, just a bit of rats; fried rats ... and maybe a bit of Schopenhauer ...

    kind regards,

    Jos ;-)

  10. #30
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by berkeleybross View Post
    That code works perfectly fine for me.
    Have you got the latest version of java etc?

    Please can you give us the exact error message produced when pq2 fails, including the full
    stack trace

    eg
    Java Code:
    run:
    pq2 is: [javaapplication14.Person1@9304b1]
    Exception in thread "main" java.lang.RuntimeException: wrong class
    Comparing
            at javaapplication14.Person1.compareTo(Main.java:55)
            at java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:582)
            at java.util.PriorityQueue.siftUp(PriorityQueue.java:574)
            at java.util.PriorityQueue.offer(PriorityQueue.java:274)
            at java.util.PriorityQueue.add(PriorityQueue.java:251)
            at javaapplication14.Main.main(Main.java:21)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    It would also help if you implemented the toString method. Since i gave it to you really have no excuse.

    How do you know if Person1@8a52b6 is dan or john?
    You may think its dan, but it *might* be john - you have no way of knowing. Until you use the toString method.

    I'm becoming a bit exasperated by your refusal to use the toString method so i refuse to help you any more until you do. You've got to help us to help you.

    By all means come back and ask for more help when you have implemented the toString method, and when you have a complete error message.
    Muhahah! It is working! I missed out the "implements Comparable" in the "class Person1. " Big kisses to Jos and berkeleybross. I assume you are guys.

    I didn't purposely left out the code you gave me. I just wanted to make the minimum work first. Now, I am going to try all the other codes you gave me. Step by step, I walk, like a baby.

  11. #31
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Glad to here you got it working!

    Just a reminder though, the code i used to implement the compareTo was just an example to get it to work for you. It may not be suitable for your case... For example what if there were two people called John? You might want to compare on name and then age, depending on what priority you need.

    Im glad to here you wern't ignoring the code on purpose, as it is fairly standard and recommended stuff.

    JosAH and Tolls i have no idea what you are talking about lol. I realise there are a lot worse than niu niu, i dont get how you two do it without getting annoyed XD

    Anyway, glad to have helped.

    Berkeleybross

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

    Default

    Quote Originally Posted by berkeleybross View Post
    JosAH and Tolls i have no idea what you are talking about lol.
    Proust, fried rats and Schopenhauer, all associated with brontosauruses; anything illogical with that?

    kindest regards,

    Jos ;-)

  13. #33
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    import acm.program.*;
    
    public class PQLoosers extends ConsoleProgram {
    	public void run ( ){	
    		
    	Person1 dan = new Person1("Dan", 44 );
    	Person1 john = new Person1("john", 50); 
    	Person1[ ] person1 = {dan, john, new Person1("Josh", 55), new Person1("Bill", 33)}; 
    		
    	PriorityQueue<Person1> pq2 = new PriorityQueue<Person1>( ); 
    	
    	for (int i=0; i<person1.length; i++) {
    		System.out.println("in run, person1[i].name is: " + person1[i].name);
    		pq2.add(person1[i]);	
    		System.out.println("in run, pq2 is: " + pq2); 
    		System.out.println(); 
    	}
    
    		System.out.println("pq2 is: " + pq2);    
        
    	    for (int i=0; i<=pq2.size()+1; i++) {
        //System.out.println("poll from pq2 is: " + pq2.poll( ).toString());    
    	}
    } 
    
    class Person1 implements Comparable  {
    	public String name; 
    	public int age;
    	
    	public Person1(String name, int age) {
    		 this.name = name; 
    		 this.age =age; 
    	}
    	
    	public String toString( ){
    		return (this.name + ":" + this.age); 
    	}
    	
    	public int compareTo(Object o) {
            // Check if o is of type person1
            if (o instanceof Person1) {
                // typecast o to a person1
                Person1 temp = (Person1) o;
                System.out.println("in CompareTo, this.name value is: " + this.name); 
                System.out.println("in CompareTo, temp.name value is: " + temp.name); 
                System.out.println("this.name.compareTo(temp.name) result is: " + this.name.compareTo(temp.name)); 
                System.out.println(); 
                return this.name.compareTo(temp.name);
            } else {
                throw new RuntimeException ("wrong class");
            }
        }
    }
    }
    --------------------------
    OUTPUT
    --------------------------
    in run, person1[i].name is: Dan
    in run, pq2 is: [Dan:44]

    in run, person1[i].name is: john
    in CompareTo, this.name value is: john
    in CompareTo, temp.name value is: Dan
    this.name.compareTo(temp.name) result is: 38

    in run, pq2 is: [Dan:44, john:50]

    in run, person1[i].name is: Josh
    in CompareTo, this.name value is: Josh
    in CompareTo, temp.name value is: Dan
    this.name.compareTo(temp.name) result is: 6

    in run, pq2 is: [Dan:44, john:50, Josh:55]

    in run, person1[i].name is: Bill
    in CompareTo, this.name value is: Bill
    in CompareTo, temp.name value is: john
    this.name.compareTo(temp.name) result is: -40

    in CompareTo, this.name value is: Bill
    in CompareTo, temp.name value is: Dan
    this.name.compareTo(temp.name) result is: -2

    in run, pq2 is: [Bill:33, Dan:44, Josh:55, john:50]

    pq2 is: [Bill:33, Dan:44, Josh:55, john:50]
    -------------------------------------------------------

    Ok, now it works and I am wondering what this CompareTo is actually doing. I know it is doing string calculation on names, as the result shows. but what does it do with the result (-40, -2 etc.) I do notice the pq2 is sorted alphabetically, but I'd like to know the exact mechanism.

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

    Default

    Quote Originally Posted by niu_niu View Post
    Ok, now it works and I am wondering what this CompareTo is actually doing. I know it is doing string calculation on names, as the result shows. but what does it do with the result (-40, -2 etc.) I do notice the pq2 is sorted alphabetically, but I'd like to know the exact mechanism.
    All it does is compare the names of two Person1 objects where Bill < Dan < Josh < john. Your priority queue considers that ordering a priority.

    kind regards,

    Jos

  15. #35
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    The rule for compareTo (which is in the javadoc) is that 0 means it's equal, less than zero means it's before and greater than zero means after. The actual values are irrelevant.

    Note: I might have the before and after back to front. I always have to run it to check...:)

  16. #36
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by berkeleybross View Post
    ... i dont get how you two do it without getting annoyed XD
    Insanity has its own rewards...wibble.
    :D

  17. #37
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Can i point out that the toString method of a priority queue is not guarenteed to return the right order.

    Say for instance, you put these values into the queue:
    Java Code:
    Person1[] person1 = {dan, john, new Person1("Dan", 55), new Person1("john", 33)};
    the result of the code would be
    Java Code:
    run:
    in run, person1[i].name is: Dan
    in run, pq2 is: [Dan:44]
    
    in run, person1[i].name is: john
    in CompareTo, this.name value is: john
    in CompareTo, temp.name value is: Dan
    this.name.compareTo(temp.name) result is: 38
    
    in run, pq2 is: [Dan:44, john:50]
    
    in run, person1[i].name is: Dan
    in CompareTo, this.name value is: Dan
    in CompareTo, temp.name value is: Dan
    this.name.compareTo(temp.name) result is: 0
    
    in run, pq2 is: [Dan:44, john:50, Dan:55]
    
    in run, person1[i].name is: john
    in CompareTo, this.name value is: john
    in CompareTo, temp.name value is: john
    this.name.compareTo(temp.name) result is: 0
    
    in run, pq2 is: [Dan:44, john:50, Dan:55, john:33]
    
    pq2 is: [Dan:44, john:50, Dan:55, john:33]
    BUILD SUCCESSFUL (total time: 0 seconds)
    To get the right order, you have to actually remove items from a priority queue. The priority queue actually does a few more compareTo's when removing items.

    Berkeleybross

  18. #38
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    got it. so where can I find the code for this CompareTo thing? it is so mysterious.

  19. #39
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

  20. #40
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    [QUOT
    To get the right order, you have to actually remove items from a priority queue. The priority queue actually does a few more compareTo's when removing items.

    Berkeleybross[/QUOTE]

    Where can I find the CompareTo code? it is not obvious to me how it is doing a few compareTo's.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. priority
    By simorgh in forum Threads and Synchronization
    Replies: 4
    Last Post: 01-07-2012, 12:49 AM
  2. Priority Queue Question
    By Taz_84 in forum New To Java
    Replies: 0
    Last Post: 01-29-2009, 03:23 AM
  3. How to implement Priority queue with Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:49 PM
  4. How to get/set thread priority
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:40 PM
  5. Java experts needed- 30 minute online Java projects
    By michelle in forum Jobs Offered
    Replies: 0
    Last Post: 03-05-2008, 11:47 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
  •