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:
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.
Step by Step Answer:
Introduction To Programming With Java A Problem Solving Approach
ISBN: 9781260575248
3rd International Edition
Authors: John Dean