
02-09-2010, 09:49 AM
|
|
Member
|
|
Join Date: Nov 2009
Location: California
Posts: 40
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?
|
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, 09:52 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
|
The "parameter" is an already instantiated Card Object. You need only compare it's values to the values from "this".
|
|

02-09-2010, 09:54 AM
|
|
Member
|
|
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
|
|
|
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, 09:55 AM
|
|
Member
|
|
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
|
|
|
Sorry, what is "this"?
Is it the card that was made in my constructor class originally?
|
|

02-09-2010, 09:56 AM
|
|
Senior Member
|
|
Join Date: Aug 2009
Posts: 1,895
Rep Power: 2
|
|
|
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, 10:00 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
"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:
|
Code:
|
Card a = new Card();
Card b = new Card();
int result = a.compareTo(b); |
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).
|
|

02-09-2010, 10:08 AM
|
|
Member
|
|
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
|
|
|
Ok, I think I got it.
Thanks everyone!
|
|

02-09-2010, 10:09 AM
|
|
Member
|
|
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
|
|
|
+1 rep for everyone
|
|

02-09-2010, 10:16 AM
|
|
Member
|
|
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
|
|
does this look right for the compareTo() method?
|
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, 10:18 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
|
Not terribly effecient, but it will work.
|
|

02-09-2010, 10:22 AM
|
|
Member
|
|
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
|
|
|
ok, thanks
|
|

02-09-2010, 03:24 PM
|
|
Member
|
|
Join Date: Feb 2010
Posts: 57
Rep Power: 0
|
|
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:
|
Code:
|
public int compareTo(Card c) {
return getRankNum() - c.getRankNum();
} |
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 of
|
Code:
|
if(a.compareTo(b) == -1) //testing, if a is less than b |
you now do this:
|
Code:
|
if(a.compareTo(b) < 0) |
Another benefit to this way of comparing the cards is, you know how far apart they are, if you happen to need this information.
|
|

02-09-2010, 04:50 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,237
Rep Power: 3
|
|
|
... also, given your code, better compare the Card's 'value' because their rank doesn't take a Card's suit in account.
kind regards,
Jos
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 03:00 AM.
|
|