Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: CardGuesser. Stuck on Card class HELP!

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question CardGuesser. Stuck on Card class HELP!

    Hey guys i'm in a bit of a pickle.
    I am quite new to Java programming but I am capable of basic tasks. I'm creating a CardGuessing game which includes 3 classes; Game,Pack and Card. In the Card class I have 2 attributes which hold the card's suit and value. Within this class I have several methods that do different things.

    I am not sure how I can implement the compareSuit and value method. Any advice would help.
    Although I wouldn't like to, I have posted the class Card as so if anyone can help, the rest of the code might help. Thanks.
    public class Card
    {
        private char suit;    // The suit of the card (C,D,H,S)
        private char value;    // The value of the card (A,2,3...10,J,Q,K)
        private String SuitName; // The suit's name(eg, "Heart")
        private String ValueName; // The Value's name (eg, "ace")
     
        /**
         * Constructor for objects of class Card
         * Creates a Card object with the parameter values as attribute values
         */
     
          Card(char suit, char value)
        {
             this.suit = suit;
             this.value = value;
        }
     
       /**
         * Grabs the required char attribute from value.
         * 
         * @return     Value of the Card
         */
     
        public char getValue()
        {
           return value;
        }
     
        /**
         * Grabs the required char attribute from suit.
         *
         * @return     Suit of the Card
         */
     
        public char getSuit()
        {
            return suit;
        }
     
       /**
         * Sets/changes the value of the Card.
         * 
         * @param  char value   Asks for the attribute in value as a character
         */
     
        public void setValue(char value)
        {
            this.value = value;
        }
     
       /**
         *Sets/changes the suit of the Card.
         * 
         * @param  char suit  Asks for the attribute in suit as a character
         */
     
        public void setSuit(char suit)
        {
            this.suit = suit;
        }
     
        /**
         *This method returns the suit of the Card as a String.
         * (eg "Diamond")
         * @return     The suit of the Card as a String. 
         */
     
        public String getSuitName()
        {
             if(suit == 'C')
             {
                 this.SuitName = "Clubs";
                }
                 else if(suit == 'D')
             {
                 this.SuitName = "Diamonds";
                }
             else if(suit == 'H')
             {
                 this.SuitName = "Hearts";
                }
             else if(suit == 'S')
             {
                 this.SuitName = "Spades";
                }
     
            return SuitName;
        }
     
     ]  /**
         * This method returns the value of the Card as a Sting
         * (eg "King" or "6")
         * @return     The value of the Card as a String.
         */
        public String getValueName()
        {
            if(value == 'A')
             {
                 this.ValueName = "Ace";
                }
                 else if(value == 'J')
             {
                 this.ValueName = "Jack";
                }
                 else if(value == 'Q')
             {
                 this.ValueName = "Queen";
                }
                 else if(value == 'K')
             {
                 this.ValueName = "King";
                }
            return ValueName;
        }
     
        /**
         * Returns - 1 if anotherCard's suit is before, 0 if equal to and +1 if after.
         * The order is 'C','D','H','S'.
         * 
         * @param  char anotherCardsSuit   
         * @return     
         */
        public int compareSuit(char anotherCardsSuit)
        {
     
        }
     
     public int compareValue(char anotherCardsValue)
        {
            return value;
        }
     
       /**
         * 
         * @param  Card anotherCard 
         * @return     True if the Cards have the same suit and value; 
         * false otherwise
         */
        public boolean equals(Card anotherCard)
        {
          return true;
        }
    Last edited by Stormin; August 11th, 2010 at 03:57 PM.


  2. #2
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: CardGuesser. Stuck on Card class HELP!

    Quote Originally Posted by Stormin View Post
    I am not sure how I can implement the compareSuit and value method. Any advice would help.
    i don't really understand what you mean with 'before' but you could use this example

    	/**
    	 * Returns - 1 if anotherCard's suit is before, 0 if equal to and +1 if
    	 * after. The order is 'C','D','H','S'.
    	 * 
    	 * @param char anotherCardsSuit
    	 * @return
    	 */
    	public int compareSuit(char anotherCardsSuit) {
    		if (anotherCardsSuit < this.suit) {
    			return -1;
    		} else if (anotherCardsSuit > this.suit) {
    			return 1;
    		}
    		return 0;
    	}

    since the order is 'C','D','H','S' the letters are in a natural order. is 'C' before 'D'? sorry, but i'm not a card player.

  3. The Following User Says Thank You to j2me64 For This Useful Post:

    Stormin (August 11th, 2010)

  4. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: CardGuesser. Stuck on Card class HELP!

    Notice anything significant about this:
    /**
    * Returns - 1 if anotherCard's suit is before, 0 if equal to and +1 if after.
    * The order is 'C','D','H','S'.
    *
    * @param char anotherCardsSuit
    * @return
    */
    The order of the suits is in alphabetical order. That makes it easy. You can compare chars using <, >, == since chars are effectively numerical values. So, you can get this card's char suit, you have the other card's char suit, so just compare them and return -1 if the other card's char suit is less than this card's char suit, 0 if the two char suits are equal, and 1 if the other card's suit is greater than this card's suit.

    You can do the same thing with compareValue.

  5. #4
    Junior Member
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: CardGuesser. Stuck on Card class HELP!

    Thanks for the replies guys!
    Would this work with compareValue though as It contains int and char so is there a way to compare in this order?
    (A,2,3,4,5,6,7,8,9,10,J,Q,K)... Btw this is the relevant order I have to use.

    I just noticed 10 would be a String right??... what parameter would I use if so?
    The assignment states I have to use the method:

    public void setValue( char value)


    However 10 wouldn't pass as a char right. If im being stupid here I can only apologise for being an idiot
    however I appreciate the help.
    Last edited by Stormin; August 13th, 2010 at 06:55 AM.

  6. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: CardGuesser. Stuck on Card class HELP!

    Sorry the list would be all char except for 10 right? Way to get around this? cheers

  7. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: CardGuesser. Stuck on Card class HELP!

    I just noticed 10 would be a String right??... what parameter would I use if so?
    To convert 10 to a char?
    char c = (char)10;

  8. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: CardGuesser. Stuck on Card class HELP!

    Quote Originally Posted by aussiemcgr View Post
    To convert 10 to a char?
    char c = (char)10;
    Where would I state this.

    CompareValues seems to work fine excpet when I compare against 'A'.
    If I compare it to '2' it thinks that 'A' is higher, however 'A' is lower(Ace is low). I've reasearched and read alot
    but nothing is sticking out to me :S

  9. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: CardGuesser. Stuck on Card class HELP!

    You'll have to make a special case for A, where you assume A to always be the lowest value. The problem is that char sets numbers before letters when it compares them. So instead of just comparing the values, you first want to find out if either value is an A. Then you want to see if the other value is an A. If both are As, they are equal, if they both arent As, then the value that is not an A is largest.

  10. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Stormin (August 11th, 2010)

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CardGuesser. Stuck on Card class HELP!

    Another way to compare the rank of a card is by having a String with the face values in order: "23456789TJQKA"
    and use the value returned by indexOf(). The 10 card would require special handling to change it to a "T".

  12. The Following User Says Thank You to Norm For This Useful Post:

    Stormin (August 11th, 2010)

  13. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: CardGuesser. Stuck on Card class HELP!

    Alternatively, you can use enumerations.

    Enumerations have an order to them and you can easily use this to quickly compare the ranking of the card. Enumerations in Java also have the added benefit of being able to easily convert between their string name and their ordinality.

    For example:

    public enum CardValue
    {
        ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING;
    }

    Then, to use these handy built-in features of enums:

    // getting the enum type by ordinality
    myCard1.value = CardValue.values()[0]; // ace
    myCard2.value = CardValue.values()[1]; // two
    // .. etc
     
    // getting the string name of the enum
    System.out.println(myCard1.value); // prints out "ACE"
    System.out.println(myCard2.value); // prints out "TWO"
     
    // comparing two cards
    myCard1.compareTo(myCard2); // result=-1 because ACE's ordinal value is 1 less than TWO's
     
    // getting the card's enum type from a string
    // note that this is case sensitive and the string must exactly match the name of the enum you want
    myCard3.value = CardValue.valueOf("KING"); // king

    Similarily, you can provide an enum type for the card suit.

    For more information about enumerations in Java: Enum Types (The Java Tutorial)

  14. The Following User Says Thank You to helloworld922 For This Useful Post:

    Stormin (August 11th, 2010)

Similar Threads

  1. I am stuck
    By hawkman4188 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 29th, 2010, 12:46 AM
  2. Card and CardTest
    By etidd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2010, 10:37 AM
  3. my programs- Card and CardTest
    By etidd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 27th, 2010, 08:06 PM
  4. I'm stuck
    By iank in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2009, 10:21 AM
  5. Method for Cedit card check, help!!!!1
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 31st, 2009, 09:16 AM