Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2010, 09:49 AM
Member
 
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
blueduiker is on a distinguished road
Talking 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.

		}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-09-2010, 09:52 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
The "parameter" is an already instantiated Card Object. You need only compare it's values to the values from "this".
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-09-2010, 09:54 AM
Member
 
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
blueduiker is on a distinguished road
Default
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.

		}  
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-09-2010, 09:55 AM
Member
 
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
blueduiker is on a distinguished road
Default
Sorry, what is "this"?
Is it the card that was made in my constructor class originally?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-09-2010, 09:56 AM
Senior Member
 
Join Date: Aug 2009
Posts: 1,895
Rep Power: 2
r035198x is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-09-2010, 10:00 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
"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).
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-09-2010, 10:08 AM
Member
 
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
blueduiker is on a distinguished road
Default
Ok, I think I got it.
Thanks everyone!
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-09-2010, 10:09 AM
Member
 
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
blueduiker is on a distinguished road
Default
+1 rep for everyone
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 02-09-2010, 10:16 AM
Member
 
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
blueduiker is on a distinguished road
Default
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;
		}
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 02-09-2010, 10:18 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
Not terribly effecient, but it will work.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 02-09-2010, 10:22 AM
Member
 
Join Date: Nov 2009
Location: California
Posts: 40
Rep Power: 0
blueduiker is on a distinguished road
Default
ok, thanks
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 02-09-2010, 03:24 PM
Member
 
Join Date: Feb 2010
Posts: 57
Rep Power: 0
m00nchile is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 02-09-2010, 04:50 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,237
Rep Power: 3
JosAH is on a distinguished road
Default
... 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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
methods lilac87 New To Java 7 07-22-2009 07:37 PM
How to get methods to see variables in other methods ejs7597 New To Java 4 04-03-2009 07:36 AM
Trouble with static methods and boolean equals() methods with classes dreamingofgreen New To Java 5 07-22-2008 07:34 AM
JSP methods example Java Tip Java Tips 0 01-30-2008 11:00 AM
methods Zensai New To Java 10 12-03-2007 06:31 AM


All times are GMT +2. The time now is 03:00 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org