Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Part B: A Deck of Cards A standard deck of playeng cards includes 52 cards - 13 each in clubs, damonds, spades, and hearts. The

image text in transcribed
image text in transcribed
image text in transcribed
Part B: A Deck of Cards A standard deck of playeng cards includes 52 cards - 13 each in clubs, damonds, 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 instancs variables: - Pace - An int value 0-12 (This works better with arrays than 1-13) 0 represents the Ace 1 represents 2 2 represents 3 10 represents the Jack 11 represents the Queen 12 rejresents the King - suit - An int value 0-3 - chubs, diamonds, spades, hearts in that order Include a standard constructor, as well as getF acel) and getSuit] methods that return the int values held in the instance variables. The toString0 method for the Card dass works diflerently from how we normally define this method. Simply return a short representation of the card, for ecample: - "Ah" for the ace of hearts - "3od for the three of diamonds - "10c" for the 10 of clubs - "Os" for the queen of spades As you can see, the String value reterned from toString() is ahways 2 or 3 characters in kength. Hint This toString 0 method becomes super easy to write if you include two arrays in your Card clars: - Plus a similar arrary caled YNCELITTER with 13 short sarings Do you remember what the word 'static' means in that conlext? il means that each indinidual Card object does not require its own copy of suzr. Etr+a. It's enough bo have one oopy of that array that is shared between any Card objects you happen to creafo. Alse write a Deck class. The Deck class has two instance varwbles. - 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 al 52 cards have been dealt, this number becomes 52 , thereatler indicating that there are no more cards to deal (at least untll the deck is shulfiod again). The Deck constructor has no parameter. At loops to create al 52 Cactis in a standard deck and inserts them ints the array in the order of Ace-to-King, of clubs, diamonds, spades, hearts, in that order. Thus the order is Ac, 20 . 3c - all the way to Qh, Kh. in terms of the (face, sut) values for the Cands. thaf's the same as (0.0),(1,0),(2.0) at the way 10(11,3),(12,3). If s those face and suit int values you need to generale to create the Card objects, since that is what the Card construclor accepts as parameters. The Deck class includes the following methods: - shuffle0 - reorders the cards within the Deck and resets ourrentCard to zero. Use the following agoorithm for shuifing your deck: for each Card in the deck. gwap this card with a card in a randomly selected position, This means you only have to loog through the cards once to shuffle 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 39h 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 dealCard0. 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 DeckTest. The order of the cards in your output should differ, of course, since each shuffle is random. Your output from Decktest should resemble the following: AcAdAsAhUnshuffledDeck2c2d2s2h3c3d3s3h4c4d4s4h5c5d5s5h6c6d6s6h7c7d7s7h8c8d8s8h9c9d9s9h10c10d10s10hJcJdJsJhQcQdQsQhKeKdKsKh *** Dealing the first five cards 2dAc Jc 2h Jd 6d Attempting to deal one more Correct - dealCard() returned nu11 Re-shuffled Deck **** Qc Jd 2h8h5h3sAc6s6h Kc 7d6d 4d Ah Ks Js Jh As 5d d 7 c 10d s hh10c9d4h 2d6cQs Kh 8d Ad 4s8s3d3c Jc 7h Qd 10h2s7s5c9c8c2c9s Kd 4c10s 9h 3h Qe Jd 2h8h5h

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions