| | |
compareTo Method
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
In my driver this is what I have
it keeps tieing.
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.
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.
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.
Here's what I have now Im getting error array out of bounds
Java Syntax (Toggle Plain Text)
public int compareTo(Object otherObject) { Hand otherHand = (Hand) otherObject; Card biggestCardHand = getHand()[0]; Card biggestCardotherHand = otherHand.getHand()[0]; for ( int i = 0; i <= theHand.length; i++) /*assuming you start your array with 0 */ { if (biggestCardHand.compareTo(getHand()[i])< 0) { biggestCardHand = getHand()[i]; } if (biggestCardotherHand.compareTo(otherHand.getHand()[i])<0) { biggestCardotherHand = otherHand.getHand()[i]; } }//end loop if (biggestCardHand.compareTo(biggestCardotherHand)< 0) { return 1; } else if (biggestCardHand.compareTo(biggestCardotherHand)> 0) { return -1; } return 0; }//end compareTO
![]() |
Similar Threads
- Something about String.split("-"); problem (Java)
- Need help tweaking my program (Java)
- Homework Help (Java)
- String class methods... (Java)
- Can't compare strings after PostBack (ASP.NET)
- Comparing Strings in C# (C#)
- Help with casting (Java)
Other Threads in the Java Forum
- Previous Thread: System Taskmanager..
- Next Thread: hide the title bar
| Thread Tools | Search this Thread |
6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class classes client code compare component coordinates database detection doctype draw eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool linux list loop map method methods mobile netbeans newbie nextline oracle pong print problem program programming project recursion recursive rim scanner screen sell server set size sms sort sql string swing terminal threads time tree user web websites windows






