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.
Code Java:
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;
}
Re: CardGuesser. Stuck on Card class HELP!
Quote:
Originally Posted by
Stormin
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
Code :
/**
* 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.
Re: CardGuesser. Stuck on Card class HELP!
Notice anything significant about this:
Quote:
/**
* 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.
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.
Re: CardGuesser. Stuck on Card class HELP!
Sorry the list would be all char except for 10 right? Way to get around this? cheers
Re: CardGuesser. Stuck on Card class HELP!
Quote:
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;
Re: CardGuesser. Stuck on Card class HELP!
Quote:
Originally Posted by
aussiemcgr
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
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.
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".
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:
Code Java:
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:
Code Java:
// 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)