Results 1 to 13 of 13
Thread: methods
- 02-09-2010, 08:49 AM #1
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
methods
Hey guys so in a class that I have to make called Card, I need to make a method called compareTo(). How do I instantiate the parameter which is Card c?
Java Code:/*returns 1 if this card has higher value, returns -1 if c has higher value, returns 0 if they have equal value */ public int compareTo (Card c) { Card c = new Card(c. }
- 02-09-2010, 08:52 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
The "parameter" is an already instantiated Card Object. You need only compare it's values to the values from "this".
- 02-09-2010, 08:54 AM #3
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
Java Code:public class Card { //Instance Data public static String [] theRanks = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"}; public static String [] theSuits = {"Hearts", "Clubs", "Diamonds", "Spades"}; private int value; int suitNum; int rankNum; //Constructor - value is between 0-51 inclusive public Card (int val) { value = val; suitNum= val / 13; rankNum = val % 13; } //toString Method public String toString() { String s = getRank() + " of " + getSuit(); return s; } //returns the card's Rank Number public int getRankNum() { return rankNum; } //returns card's Suit Number public int getSuitNum() { return suitNum; } //return cards Rank in String form public String getRank() { return theRanks [rankNum]; } //returns public String getSuit() { return theSuits [suitNum]; } /*returns 1 if this card has higher value, returns -1 if c has higher value, returns 0 if they have equal value */ public int compareTo (Card c) { Card c = new Card(c. } }
- 02-09-2010, 08:55 AM #4
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
Sorry, what is "this"?
Is it the card that was made in my constructor class originally?
- 02-09-2010, 08:56 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You don't need to instantiate it. The parameter indicates that the method is going to be passed an already instantiated Card so the method has one less thing to worry about.
- 02-09-2010, 09:00 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
"this" is, of course, the object instance on which compareTo is called. It is the "current" instance of the object, so to say. The way that method will be called is as follows:
so the compareTo method of "a" will be called. So, any reference to "this" in that method will refer to the values in "a" (in that instance).Java Code:Card a = new Card(); Card b = new Card(); int result = a.compareTo(b);
- 02-09-2010, 09:08 AM #7
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
Ok, I think I got it.
Thanks everyone!
- 02-09-2010, 09:09 AM #8
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
+1 rep for everyone
- 02-09-2010, 09:16 AM #9
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
does this look right for the compareTo() method?
Java Code:/*returns 1 if this card has higher value, returns -1 if c has higher value, returns 0 if they have equal value */ public int compareTo(Card c) { int result = 9001; if (getRankNum() > c.getRankNum()) result = 1; if (getRankNum() < c.getRankNum()) result = -1; if (getRankNum() == c.getRankNum()) result = 0; return result; }
- 02-09-2010, 09:18 AM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Not terribly effecient, but it will work.
- 02-09-2010, 09:22 AM #11
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
ok, thanks
- 02-09-2010, 02:24 PM #12
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Just a tip. compareTo() doesn't need to return specific values, just a range, negative if the first object is lesser, positive if it's greater, or 0 if the two objects are equal. So to simplify, and optimize, your example, you could simply do:
So, instead of 6 calls to the getRankNum() method, you now only have 2, and when you call the compareTo() method in your program, instead ofJava Code:public int compareTo(Card c) { return getRankNum() - c.getRankNum(); }
you now do this:Java Code:if(a.compareTo(b) == -1) //testing, if a is less than b
Another benefit to this way of comparing the cards is, you know how far apart they are, if you happen to need this information.Java Code:if(a.compareTo(b) < 0)
- 02-09-2010, 03:50 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
methods
By lilac87 in forum New To JavaReplies: 7Last Post: 07-22-2009, 06:37 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM -
JSP methods example
By Java Tip in forum Java TipReplies: 0Last Post: 01-30-2008, 10:00 AM -
methods
By Zensai in forum New To JavaReplies: 10Last Post: 12-03-2007, 05:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks