Question: Research the Fisher-Yates shuffling algorithm online, then use it to reimplement the shuffle method in Fig.7.12. Fig.7.12 I // Fig. 7.12: DeckOfCards.java 2 // DeckOfCards

Research the Fisher-Yates shuffling algorithm online, then use it to reimplement the shuffle method in Fig.7.12.

Fig.7.12

I // Fig. 7.12: DeckOfCards.java 2 // DeckOfCards class represents a deck of playing cards. 3 import

I // Fig. 7.12: DeckOfCards.java 2 // DeckOfCards class represents a deck of playing cards. 3 import java.security.SecureRandom; 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 public class DeckOfCards { 52 53 } // random number generator private static final SecureRandom randomNumbers = new SecureRandom(); private static final int NUMBER_OF_CARDS = 52; // constant # of Cards private Card [] deck = new Card [NUMBER_OF_CARDS]; // Card references private int currentCard = 0; // index of next Card to be dealt (0-51) // constructor fills deck of Cards public DeckOfCards () { String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"}; } } // populate deck with Card objects for (int count = 0; countdeck.length; count++) { deck [count] = new Card(faces [count % 13], suits [count / 13]); // shuffle deck of Cards with one-pass algorithm public void shuffle() { // next call to method dealCard should start at deck [0] again currentCard = 0; } } // for each Card, pick another random Card (0-51) and swap them for (int first = 0; first < deck. length; first++) { // select a random number between 0 and 51 int second = randomNumbers.nextInt (NUMBER_OF_CARDS); } // swap current Card with randomly selected Card Card temp = deck[first]; // deal one Card public Card dealCard() { // determine whether Cards remain to be dealt if (currentCard < deck. length) { return deck[currentCard++]; // return current Card in array deck[first] = deck[second]; deck[second] = temp; } } else { return null; // return null to indicate that all Cards were dealt

Step by Step Solution

3.35 Rating (155 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The FisherYates shuffle is an algorithm for generating a random permutation of a finite sequencein p... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java How To Program Late Objects Questions!