Rank and Suit

Rank and Suit are enumeration types that represent the ranks and suits of a standard deck of playing cards. toString(): Return the string representation of the enum constant, which is assigned to the symbol field.

Card

Card represents the cards in a standard deck. Each Card object has a Rank and a Suit.

  • Card (Rank rank, Suit suit): Assign the given enum constants to the fields. If either reference is null, throw a NullPointerException.
  • compareTo(): Implement the method so that a list of Cards is sorted first by Suit and then by Rank. (Hint: Java enums implement the Comparable interface, and the compiler automatically generates their compareTo method.)
  • equals (Object obj): Override the Object equals method so that two Cards are considered equal if they have the same Rank and Suit. Do this by following these steps:
    • Check if the given object is an instance of Card. If not, return false.
    • Downcast the Object reference to Card.
    • Compare the Ranks and Suits of the Cards.
  • hashCode() : Any class that overrides equals should also override hashCode. Do this using a method of the Objects class as shown in the JUnit tests.
  • toString(): Return the concatenation of the string representations of the Rank and Suit.

Deck

Deck represents a standard deck of playing cards. Each Deck object has a List of Cards.

  • Deck() : Initialize the List so that it contains all 52 unique Cards in sorted order. (Hint: Use the enum values method, which is automatically added to Rank and Suit by the compiler.)
  • draw(): Remove and return the first Card in the List. If the List is empty, return null.
  • draw(int count): Remove and return the given number of Cards from the front of the List. If the number is larger than the size, remove and return all the Cards. If the number is less than zero, return an empty List.
  • shuffle(): Randomize the order of the Cards in the List. (Hint: Use a method of the Collections class.)
  • size() Return the number of Cards in the List.
  • toString() Return the string representation of the List.

BlackjackHand

BlackjackHand represents a hand of cards in a game of Blackjack. Objects of the class are created with two Cards that are stored in a List. More Cards can be added until the value of the hand reaches 21 or greater.

  • CARD_VALUES: A static Map from card ranks to card values. Cards with a numerical rank have the same value as the number; jacks, queens, and kings have a value of 10. Aces have a value of either 1 or 11, but a Map can only store one value with each key, so store the larger value.
  • To initialize a static field like this that requires multiple lines of code, use one of the following approaches:
    • Write a helper method that creates and returns the Map.
    • Create the Map in a static initialization block.
  • addCard (Card card): If the value of the hand is less than MAX_VALUE, add the given Card to the end of the List.
  • getCardValues() Return a copy of the card values Map.
  • getCards() Return a copy of the cards List.
  • getValue(): Return the value of the hand (i.e., the total value of all the Cards). If the hand contains aces, return the largest value that is less than or equal to MAX_VALUE. If this is not possible, return the smallest value that is greater than MAX_VALUE.
  • size() Return the number of Cards in the List.
  • toString() Return the string representation of the List.

GoFishHand

GoFishHand represents a hand of cards in a game of Go Fish. Each object of the class has a Map and a Set. The Map stores the Cards of each partial book. The Set stores the Ranks of the complete books.

  • GoFishHand (Collection cards) : Add each Card in the given Collection to the Map. If the Collection is empty, throw an IllegalArgumentException. If a book is formed while adding the Cards, remove the corresponding entry from the Map and add the Rank to the books Set.
  • addCard (Card card): Add the given Card to the Map. If a book is formed, remove the mapping and add the Rank to the books Set.
  • addCards (Collection< Card > cards): Add each Card in the given Collection to the Map. If any books are formed, update the Map and Set as described above.
  • getBooks() Return a copy of the Set.
  • getCards() Return a copy of the Map.
  • getRankCounts(): Create and return a Map with the number of Cards in each partial and complete book. Each key is a Rank, and the associated value is the number of Cards with that Rank.
  • removeCards (Rank rank) : Remove the mapping for the given Rank and return the associated Set of Cards. If the mapping does not exist, return an empty Set.
  • size() : Return the total number of Cards in the Map.
  • toString() Return a two-line string representation of the hand. The first line consists of the string representation of the Map with the prefix "CARDS: ". The second line consists of the string representation of the Set with the prefix "BOOKS: "
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.