import java.util.Random; /** * Card.java * A class that represents a standard playing Card */ public class Card { private int suit; private int rank; private static Random gen = new Random(); /** * Card() * Input: none * Output: none * Description: Creates a Card object with a random * rank and a random suit */ public Card() { suit = gen.nextInt(4) + 1; rank = gen.nextInt(13) + 2; } /** * Card(int r, int s) * Input: int r - an int between 2 and 14 * int s - a suit between 1 and 4 * Output: none * Description: Creates a Card object with a rank r * and a suit s */ public Card(int r, int s) { if(r > 14 || r < 2) rank = 2; else rank = r; if(s > 4 || s < 1) suit = 1; else suit = s; } /** * getSuit() * Input: none * Output: an integer that has the value suit * Description: gets the integer representation of the suit */ public int getSuit() { return suit; } /** * setSuit(int newSuit) * Input: an integer between 1 and 4 * Output: none * Description: sets the suit to a value if the input is * in the appropriate range; otherwise sets it to 1 */ public void setSuit(int newSuit) { if(newSuit >=1 && newSuit <= 4) suit = newSuit; else suit = 1; } /** * getRank() * Input: none * Output: an integer that has the value suit * Description: gets the integer representation of the rank */ public int getRank() { return rank; } /** * setRank(int newRank) * Input: an integer between 2 and 14 * Output: none * Description: sets the rank to a value if the input is * in the appropriate range; otherwise sets it to 2 */ public void setRank(int newRank) { if(newRank >= 2 && newRank <= 14) rank = newRank; else rank = 2; } /** * isHigher(Card c) * Input: a Card object c * Output: a boolean * Description: checks to see if c is higher than this card. * It first checks the rank, if the rank is higher it returns true. * If it is lower, it returns false. If they are the same, it checks * the suit and returns true if the suit is higher, false otherwise. */ public boolean isHigher(Card c) { if(this.getRank() > c.getRank() || (this.getRank() == c.getRank() && this.getSuit() > c.getSuit())) return true; else return false; }//isHigher /** * toString() * Input: none * Output: a String representation of this object * Description: Creates a String representation of the card * by determining its rank and then its suit. */ public String toString() { String result = ""; switch(this.getRank()) { case 14: result += "A "; break; case 13: result += "K "; break; case 12: result += "Q "; break; case 11: result += "J "; break; case 10: result += "10"; break; default: result += rank + " "; } result += " of "; switch(this.getSuit()) { case 1: result += "Clubs"; break; case 2: result += "Diamonds"; break; case 3: result += "Hearts"; break; case 4: result += "Spades"; break; } return result; }//end toString() }//end class