Shuffling: Suppose you are developing a card game program like that suggested earlier in the chapter, but

Question:

Shuffling:

Suppose you are developing a card game program like that suggested earlier in the chapter, but you want to use an ArrayList instead of an array. The following UML class diagram shows your plan:

DeckDriver +main(args: String[]): void GroupOfCards -cards : ArrayList +getCards(): ArrayList +addCard(card :

Assume you have implemented the Card class and the GroupOfCards class. (The next exercise will ask you to do this.) Assume that the addCard method adds its passed-in card to the end of the ArrayList. Assume that the removeCard method deletes the passed-in card from the ArrayList (reducing the ArrayList’s size by one) and returns a reference to the removed card. Provide Java code for the Deck and DeckDriver classes. In your Deck class, you must implement a shuffle method that relies on the addCard and removeCard methods. You are not allowed to use the Collection class’s shuffle method. When you execute the driver, you should get output like that in the following sample session, with randomness altering the output after shuffling:

Sample session: Unshuffled Deck 2 of clubs 3 of clubs Deck After Shuffling 14 of spades 3 of hearts

To shuffle the deck, in the Deck class’s shuffle method, use a for loop that starts with unshuffled = getCards().size() and steps down to 1. In each iteration, use Math.random to pick an index in the unshuffled range, remove the card at that index, and then add it back to the ArrayList.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Question Posted: