public class PlayingCard
{
private int faceValue;
private int suitValue;
public static final int HEARTS = 0;
public static final int DIAMONDS = 1;
public static final int SPADES = 2;
public static final int CLUBS = 3;
public static final int ACE = 1;
public static final int KING = 13;
public static final int QUEEN = 12;
public static final int JACK = 11;
/**
* Constructor for PlayingCard class.
* Generates a random playing card (randomly selected face value and suit).
*/
public PlayingCard()
{
generateNewRandomCard();
}
/**
* Constructor for PlayingCard class.
*
* @param face The face value for the card. 2 through 10 are used for the numbered cards.
* Pass one of the named constants as the value for this parameter for the
* Ace, King, Queen, or Jack.
*
* @param suit The suit of the card. Pass one of the named constants for the suit of the
* card.
*/
public PlayingCard(int face, int suit)
{
faceValue = face;
suitValue = suit;
}
/**
* Generates a random card. That is, this method can be called to select a
* new random face value and suit.
*/
public void generateNewRandomCard()
{
Random flip = new Random();
// STEP 1: Complete the following statement so that a random "suit" is selected.
// Note that we have been given constants above that will be used to
// represent each of the suits. You want to complete the following
// statement to select a random integer in the range 0 to 3.
// Recall from a previous assignment, that we can generate a random
// number between 0 and n by calling the nextInt method of the Random
// class. For example, our Random object has been called flip above,
// so we can generate a random number between 0 and n with flip.nextInt(n+1).
// In other words, if we want a random number between 0 and 9 we would
// have flip.nextInt(10) Only here, we want a random number between
// 0 and 3.
suitValue = flip.nextInt(4) ;
// STEP 2: Complete the following statement to generate a random face value for the
// Card. Face values can be Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen,
// King. We have been given constants above to represent the "named" face
// values. Specifically, we are using 1 for the Ace, 11, 12, and 13 for
// Jack, Queen, and King. 2 through 10 will just be the obvious 2 through 10.
//
// To generate a Random face value we will need a random number between
// 1 and 13. HINT: To get this, you can call nextInt, but you will need to
// add 1 to the result, just like you did with the Dice program.
faceValue = flip.nextInt(12) + 1 ;
}
/**
* Accessor to get the face value of the card as a String. Specifically,
* this method will return to you a String with one of "Ace", "2", "3", ..., "10", "Jack",
* "Queen", "King". This method should return "NOT A CARD" if the face value is invalid.
*
* @return A String with the face value of the Card
*/
public String getCardFaceValue()
{
//STEP 3: Write the Java statements necessary to determine which card is represented
// by the instance field faceValue. Return the String that describes that card.
// If the value of faceValue is not valid (not one of the 13 legal face values,
// return the string "NOT A CARD".
//
// HINT: You will need an if statement. You will likely want to specifically
// use the optional else if part as well.
// HINT 2: In the conditions of your if statement, you can compare the value of
// faceValue to the constant that represents the Ace. If those values are
// equal, have a return statement that returns "Ace". You'll then want
// an else if with a condition that checks if the card is a King, and so forth.
// HINT 3: If you determine that the card is one of the numbered values, you will
// need to return the String version of it. Recall that one way of turning
// a number into a String is to concatenate it with the empty String. That is
// If n is a variable of type int and you want to return the String version of it,
// you can do: return "" + n;
if(faceValue == 1) {
return "Ace";
}
else if(faceValue==2) {
return "Two";
}
else if(faceValue==3) {
return "Three";
}
else if(faceValue==4) {
return "Four";
}
else if(faceValue==5) {
return "Five";
}
else if(faceValue==6) {
return "Six";
}
else if(faceValue==7) {
return "Seven";
}
else if(faceValue==8) {
return "Eight";
}
else if(faceValue==9) {
return "Nine";
}
else if(faceValue==10) {
return "Ten";
}
else if(faceValue==11) {
return "Jack";
}
else if(faceValue==12) {
return "Queen";
}
else if(faceValue==13) {
return "King";
}
else if(faceValue >=13) {
return "NOT A CARD";
}
}
/**
* Accessor to get the suit of the card as a String. Specifically,
* this method will return to you a String with one of "Hearts", Spades", "Clubs", "Diamonds".
* It will return "NOT A CARD" if the suit is not one of the legal suits.
*
* @return A String representing the name of the suit of the card.
*/
public String getCardSuit()
{
//STEP 4: Write an if statement (you will likely need to use the else if part) to compare
// the value of the instance field suitValue to each of the 4 possible choices (the
// constants defined at the top). This if statement should determine which
// suit the card is from and should return the name of it as a String.
// If the value stored in suitValue is not one of the 4 legal values, return "NOT A CARD"
// (hint: this last case can be an else).
}
/**
* Accessor to get the suit as an integer.
* @return the suit as an integer between 0 and 3 that corresponds to the named constants for the suit
*/
public int getCardSuitAsInt()
{
// DO NOT CHANGE THIS METHOD IN ANY WAY
return suitValue;
}
/**
* Accessor to get the face value as an integer.
* @return the face value as an integer between 1 and 13 that corresponds to the named constants for the face value
*/
public int getCardFaceAsInt()
{
// DO NOT CHANGE THIS METHOD IN ANY WAY
return faceValue;
}
}