compareTo Method

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 36
Reputation: cassyjack is an unknown quantity at this point 
Solved Threads: 0
cassyjack's Avatar
cassyjack cassyjack is offline Offline
Light Poster

compareTo Method

 
0
  #1
Feb 28th, 2007
Hi need a little help I cant see whats wrong with this code can someone take a look at this and steer me in the right direction.

Specs say: I have to make hands comparable. I only comparing hands of the same length. One hand is bigger than another if the largest card of the first hand is larger than the largest card of the second hand:

public int compareTo(Hand otherHand)
    {
        for (int i=theHand.length; i >theHand.length; i--)
        {
            if (getHand()[i]!= otherHand.getHand()[i])
                return getHand()[i].compareTo( otherHand.getHand()[i]);
            }
            return 0;
        }


In my driver this is what I have
int biggestHand = playerHand.compareTo (computerHand);
            if (biggestHand > 0)
                JOptionPane.showMessageDialog(player wins);
            else if (biggestHand < 0)
                 JOptionPane.showMessageDialog(computers wins);
            else
                JOptionPane.showMessageDialog(tie);

it keeps tieing.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: compareTo Method

 
0
  #2
Feb 28th, 2007
apart from your compareTo method never returning anything but 0 (at least from the part you posted)?

And of course you're violating the Comparable contract by having the signature of your compareTo method take a Hand instance instead of an Object, so it's quite possible it never gets called at all.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 36
Reputation: cassyjack is an unknown quantity at this point 
Solved Threads: 0
cassyjack's Avatar
cassyjack cassyjack is offline Offline
Light Poster

Re: compareTo Method

 
0
  #3
Feb 28th, 2007
Here's what I have now Im getting error array out of bounds

  1. public int compareTo(Object otherObject)
  2. { Hand otherHand = (Hand) otherObject;
  3. Card biggestCardHand = getHand()[0];
  4. Card biggestCardotherHand = otherHand.getHand()[0];
  5.  
  6. for ( int i = 0; i <= theHand.length; i++) /*assuming you start your array with 0 */
  7. {
  8. if (biggestCardHand.compareTo(getHand()[i])< 0)
  9. {
  10. biggestCardHand = getHand()[i];
  11. }
  12. if (biggestCardotherHand.compareTo(otherHand.getHand()[i])<0)
  13. {
  14. biggestCardotherHand = otherHand.getHand()[i];
  15. }
  16. }//end loop
  17. if (biggestCardHand.compareTo(biggestCardotherHand)< 0)
  18. {
  19. return 1;
  20. }
  21. else
  22. if (biggestCardHand.compareTo(biggestCardotherHand)> 0)
  23. {
  24. return -1;
  25. }
  26.  
  27.  
  28. return 0;
  29. }//end compareTO
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: compareTo Method

 
0
  #4
Feb 28th, 2007
Use < rather than <=. I didn't really read the code, except for that bit.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC