Question: The to String() method for the Card class works differently from how we normally define this method. Simply return a short representation of the



The to String() method for the Card class works differently from how we normally define this method. Simply return a short representation of the card, for example: . "Ah" for the ace of hearts "3d" for the three of diamonds "10c" for the 10 of clubs "Qs" for the queen of spades As you can see, the String value returned from to String() is always 2 or 3 characters in length. Hint: This toString() method becomes super easy to write if you include two arrays in your Card class: private static final String [] SUITLETTER = {"c","d", "s", "h"); Plus a similar array called FACELETTER with 13 short strings. Do you remember what the word 'static' means in that context? It means that each individual Card object does not require its own copy of SUITLETTER. It's enough to have one copy of that array that is shared between any Card objects you happen to create. 837 Also write a Deck class. The Deck class has two instance variables: An array of 52 Card objects currentCard - An int value. When this number is in the range 0-51, it represents the position of the next card to be dealt from the deck. Once all 52 cards have been dealt, this number becomes 52, thereafter indicating that there are no more cards to deal (at least until the deck is shuffled again). The Deck constructor has no parameter. It loops to create all 52 Cards in a standard deck and inserts them into the array in the order of Ace-to-King, of clubs, diamonds, spades, hearts, in that order. Thus the order is Ac, 2c, 3c... all the way to Qh, Kh. In terms of the (face, suit) values for the Cards, that's the same as (0,0), (1,0), (2,0) ... all the way to (11,3), (12,3). It's those face and suit int values you need to generate to create the Card objects, since that is what the Card constructor accepts as parameters. The Deck class includes the following methods: shuffle() - reorders the cards within the Deck and resets currentCard to zero. Use the following algorithm for shuffling your deck: for each Card in the deck, swap this card with a card in a randomly selected position. This means you only have to loop through the cards once to shuffle the deck. dealCard - If currentCard is less than the number of cards, return the Card object in the position indicated by currentCard, and increase currentCard by one. Otherwise return null. Note that "dealing" a card doesn't remove it from the Deck the only change is to update currentCard to a new position. - toString() - Concatenates together the toString() results for every card in the deck. Insert two spaces between each card. Insert a newline character after the 13th, 26th, an 39th card, so that printing the result shows up as four lines of thirteen cards. You can see a few examples in the sample output shown on the next page. Also write a DeckTest class to try out your Card and Deck classes. This test class should: 1. Create a Deck and display it (using toString) in its original, unshuffled state. 2. Shuffle the deck and display it again. 3. Show the cards returned by dealing the first five cards from the deck. (You can visually compare with the shuffled deck display to ensure your dealCard() method is working properly.) 4. Loop to deal the rest of the cards in the deck, except for the very last card, but with no output. 5. Deal the last card and print it so you can confirm you are getting the last card in the shuffled deck. 6. Use an if-else statement to test one more call to dealCard(). Confirm whether or not you get back the appropriate result, and display an appropriate message either way 7. Shuffle the deck and display it again. 8. Deal 5 more cards and display them, which allows you to confirm that the shuffle reset the value of currentCard appropriately. See the next page for sample output from my version of Deck Test. The order of the cards in your output should differ, of course, since each shuffle is random. Part B: A Deck of Cards A standard deck of playing cards includes 52 cards - 13 each in clubs, diamonds, spades, and hearts. The 13 cards in each suit are the Ace, 2 through 10, Jack, Queen, and King. Write a Card class that represents a single playing card. This class has two instance variables: face - An int value 0-12 (This works better with arrays than 1-13) O represents the Ace 1 represents 2 2 represents 3 10 represents the Jack 11 represents the Queen 12 represents the King suit - An int value 0-3 - clubs, diamonds, spades, hearts in that order 1030 Include a standard constructor, as well as getFace() and getSuit() methods that return the int values held in the instance variables. Your output from Decktest should resemble the following: *** Unshuffled Deck *** Ac Jc Ad 2c 3c 4c 5c 6c 7c 8c 9c 10c JC Qc Kc 2d 3d 4d 5d 6d 7d 8d 9d 10d Jd Qd Kd As 2s 3s 3s 4s 5s 6s 7s 8s 9s 10s Js Qs Ks Ah 2h 3h 4h 5h 6h 7h 8h 9h 10h Jh Qh Kh **** Shuffled Deck **** 9s Jd 2d Ac Jc 2h 10s Kd 3s 5s 2c Qs 4d Ah 5h 10h Qd 6c 5c 7s 8d 5d sa 3c 9h 3d 4c Jh Ks 10d 7c 4s Ad 9c Js 2s 4h Qc Kc 10c 3h 6h 8s 9d 7d 8h As 7h Qh 6s 8c Kh 6d **** Dealing the first five cards **** 2d Ac Jc 2h Jd **** Dealing the last card from the deck **** 6d **** Attempting to deal one more **** Correct dealCard () returned null Ah 2d **** Re-shuffled Deck **** 5h 3s Ac Ac 6s 6h Kc 7d 6d 4d Qc Jd 2h 8h Ks Js Jh As 5d 7c 10d 5s Qh 10c 9d 4h 6c Qs Kh 8d Ad 4s 8s 3d 3c Jc 7h Qd 2s 7s 5c 9c 8c 2c 9s Kd 4c 10s 9h 3h 10h **** Dealing the first five cards **** Qc Jd 2h 8h 5h Bonia Cux keb BE362 sritis tuo
Step by Step Solution
There are 3 Steps involved in it
The word static in the context of the Card class means that the arrays SUITLETTER and FACELETTER are ... View full answer
Get step-by-step solutions from verified subject matter experts
