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
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 :](https://dsd5zvtm8ll6.cloudfront.net/images/question_images/1704/8/7/1/932659e47fca06a71704871931139.jpg)
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:

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.
DeckDriver +main(args: String[]): void GroupOfCards -cards : ArrayList +getCards(): ArrayList +addCard(card : Card) : void +removeCard(card: Card) : Card +display(heading: String): void Deck +TOTAL_CARDS: int=52 +Deck() +shuffle(): void -num : int -suit: int * Card +Card(num : int, suit : int) +display() : void
Step by Step Solution
3.47 Rating (157 Votes )
There are 3 Steps involved in it
Based on the UML class diagram provided and your description of the task I will implement the Deck class and the DeckDriver class in Java The Deck cla... View full answer
Get step-by-step solutions from verified subject matter experts
