Anon017706349

aaacard

Sep 26th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1.  
  2. /*
  3. An object of class card represents one of the 52 cards in a
  4. standard deck of playing cards. Each card has a suit and
  5. a value.
  6. */
  7.  
  8.  
  9. public class Card {
  10.  
  11. public final static int SPADES = 0, // Codes for the 4 suits.
  12. HEARTS = 1,
  13. DIAMONDS = 2,
  14. CLUBS = 3;
  15.  
  16. public final static int ACE = 1, // Codes for the non-numeric cards.
  17. JACK = 11, // Cards 2 through 10 have their
  18. QUEEN = 12, // numerical values for their codes.
  19. KING = 13;
  20.  
  21. private final int suit; // The suit of this card, one of the constants
  22. // SPADES, HEARTS, DIAMONDS, CLUBS.
  23.  
  24. private final int value; // The value of this card, from 1 to 11.
  25.  
  26. public Card(int theValue, int theSuit) {
  27. // Construct a card with the specified value and suit.
  28. // Value must be between 1 and 13. Suit must be between
  29. // 0 and 3. If the parameters are outside these ranges,
  30. // the constructed card object will be invalid.
  31. value = theValue;
  32. suit = theSuit;
  33. }
  34.  
  35. public int getSuit() {
  36. // Return the int that codes for this card's suit.
  37. return suit;
  38. }
  39.  
  40. public int getValue() {
  41. // Return the int that codes for this card's value.
  42. return value;
  43. }
  44.  
  45. public String getSuitAsString() {
  46. // Return a String representing the card's suit.
  47. // (If the card's suit is invalid, "??" is returned.)
  48. switch ( suit ) {
  49. case SPADES: return "Spades";
  50. case HEARTS: return "Hearts";
  51. case DIAMONDS: return "Diamonds";
  52. case CLUBS: return "Clubs";
  53. default: return "??";
  54. }
  55. }
  56.  
  57. public String getValueAsString() {
  58. // Return a String representing the card's value.
  59. // If the card's value is invalid, "??" is returned.
  60. switch ( value ) {
  61. case 1: return "Ace";
  62. case 2: return "2";
  63. case 3: return "3";
  64. case 4: return "4";
  65. case 5: return "5";
  66. case 6: return "6";
  67. case 7: return "7";
  68. case 8: return "8";
  69. case 9: return "9";
  70. case 10: return "10";
  71. case 11: return "Jack";
  72. case 12: return "Queen";
  73. case 13: return "King";
  74. default: return "??";
  75. }
  76. }
  77.  
  78. public String toString() {
  79. // Return a String representation of this card, such as
  80. // "10 of Hearts" or "Queen of Spades".
  81. return getValueAsString() + " of " + getSuitAsString();
  82. }
  83.  
  84.  
  85. } // end class Card
Add Comment
Please, Sign In to add comment