Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this lab, we will use the Java collections framework to write classes that represent collections of playing cards from a standard 52-card deck.
In this lab, we will use the Java collections framework to write classes that represent collections of playing cards from a standard 52-card deck. Additionally, we will use enumeration types to represent the ranks and suits of the cards. Below is a UML diagram with the details. enumeration"> Rank TWO("2") THREE ("3") FOUR("4") FIVE("5") SIX("6") SEVEN("7") EIGHT("8") NINE ("9") TEN("10") JACK("J") QUEEN("Q") KING("K") ACE ("A") -symbol: String -Rank (symbol: String) +toString(): String) -cards: List +Deck() +size(): int Deck interface Comparable +compareTo(o: T): int -rank: Rank -suit: Suit 1 Card +Card(Rank rank, Suit suit) +compareTo(Card card): int +equals(Object obj): boolean +getRank (): Rank +getSuit(): Suit +hashCode (): int +toString(): String +toString(): String +draw(): Card +draw(int count): List +shuffle(): void +getCardsByRank (Rank rank): List enumeration>> Suit CLUBS("C") DIAMONDS ("D") HEARTS("H") SPADES ("S") -symbol: String -Suit (symbol: String) +toString(): String BlackjackHand -CARD VALUES: Map -MAX VALUE = 21: int -cards = new ArrayList(): List -value = 8: int -numAcesAs11 = 0: int +BlackjackHand (Card c1, Card c2) +addCard(Card card): void +size(): int +getCardValues(): Map +getCards (): List +getValue(): int +toString(): String Before starting this assignment, you may want to review the following material: zyBook: 15.3, 15.5, and 15.6 Oracle Java Tutorial: Enum Types 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 compare To 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: i. Check if the given object is an instance of Card. If not, return false. ii. Downcast the Object reference to Card. iii. Compare the Ranks and Suits of the Cards. Deck 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 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. (Hint: List implementations include a tostring() method that returns a string representation of the list.) get Cards By Rank (Rank rank) : Return the list of cards in the deck, with the corresponding argument rank. 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, create the Map in a static initialization block. addCard (Card card): First, check if the card is null, throw a NullPointerException. If the current value of the hand is less than MAX_VALUE, add the given Card to the end of the List. Make sure you update numAcesAs11 if the card rank is Ace. Also, you must update the value field whenever you add a card. 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). While the value is greater than MAX_VALUE and the hand contains aces, choose the smaller value (i.e., 1) for Aces to minimize the total value. size(): Return the number of Cards in the List. toString(): Return the string representation of the List.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Algorithm of the code Define two enumeration types Rank and Suit representing the ranks and suits of playing cards respectively Implement the Card class which represents a single playing card Define i...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started