Results 1 to 13 of 13

Thread: methods

  1. #1
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    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?

    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.
    
    		}

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    The "parameter" is an already instantiated Card Object. You need only compare it's values to the values from "this".

  3. #3
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    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.
    
    		}  
    }

  4. #4
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    Sorry, what is "this"?
    Is it the card that was made in my constructor class originally?

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    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.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    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:
    Java 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).

  7. #7
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    Ok, I think I got it.
    Thanks everyone!

  8. #8
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    +1 rep for everyone

  9. #9
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    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;
    		}

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Not terribly effecient, but it will work.

  11. #11
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    ok, thanks

  12. #12
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    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:
    Java 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
    Java Code:
    if(a.compareTo(b) == -1) //testing, if a is less than b
    you now do this:
    Java 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.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    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

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. methods
    By lilac87 in forum New To Java
    Replies: 7
    Last Post: 07-22-2009, 06:37 PM
  3. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  4. JSP methods example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 10:00 AM
  5. methods
    By Zensai in forum New To Java
    Replies: 10
    Last Post: 12-03-2007, 05:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •