Research the Fisher-Yates shuffling algorithm online, then use it to reimplement the shuffle method in Fig.7.12. Fig.7.12
Question:
Research the Fisher-Yates shuffling algorithm online, then use it to reimplement the shuffle method in Fig.7.12.
Fig.7.12
Transcribed Image Text:
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
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 0% (1 review)
The FisherYates shuffle is an algorithm for generating a random permutation of a finite sequencein p...View the full answer
Answered By
Ankit Mahajan
I am an electrical engineering graduate from Thapar institute of engineering and technology.
Qualified exams - GATE 2019,2020.
CAT EXAM 2021- 91.4 percentile
SSC EXAMS- 2019,2020,2021
AFCAT EXAM- 2019,2020,2021
I want to share my knowledge with other people so that they can achieve the same.
I have strong hold Mathematics, Electrical engineering and all the subjects related.
Just give me a problem and I will give you the solution of it.
5.00+
1+ Reviews
10+ Question Solved
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Question Posted:
Students also viewed these Computer science questions
-
Write a literature review for your study. See below for an example of a literature review. Your literature review should provide both analysis and synthesis of previous studies as related to the...
-
In the following problems properties of the Laplace transform are used. (a) Show that the Laplace transform of x(t) e at u(t)is X(s + a), where X(s) = L[x(t)] and then use it to find the Laplace...
-
Implement a bogus sorting algorithm called bogo sort that uses your shuffling algorithm from the previous exercise to sort an array of numbers. The bogo sort algorithm is the following: Obviously,...
-
Elm Corp. makes two products: C and D. The following data have been summarized: Indirect manufacturing cost information includes the following: The company plans to manufacture 250 units of each...
-
Differentiate between an originating temporary difference and a reversing difference.
-
Rossi Industries has payroll processes as described in the following paragraphs: When a new employee is hired, the human resources department completes a personnel action form and forwards it to the...
-
Explain how the measurement of a liability is related to its cause.
-
Selected information from the Blake Corporation accounting records for June follows: Additional information for July follows: Labor wage rate was $35 per hour. During the month, sales revenue was...
-
Wilcox Mills is a manufacturer that makes all sales on 30-day credit terms. Annual sales are approximately $30 million. At the end of year 1, accounts receivable were presented in the company's...
-
While exercising, you can use a heart-rate monitor to see that your heart rate stays within a safe range suggested by your trainers and doctors. According to the American Heart Association (AHA)...
-
Modify the application of Figs. 7.117.13 to use Face and Suit enum types to represent the faces and suits of the cards. Declare each of these enum types as a public type in its own source-code file....
-
California is working on an intelligent highway system that would allow computer-controlled automobiles to travel faster and closer together on freeways than todays human-controlled cars. What kinds...
-
1) You are swinging a bucket full of water in a vertical circle. The radius of the circle is 150 cm and the bucket and water have a total weight of 30 N. When the bucket is at the bottom of its...
-
A student walking to class on a cold day ( T o = 5 C) find a silver ring with an inner diameter of D = 1.7 cm. The silver has a coefficient of linear expansion of = 19.7 10 -6 1/C. Part A. Input an...
-
A roller coaster car (RC) of mass m = 1200 kg (includes passengers) is to be thrust into motion from a height H with a speed of vo = 2 m/s. It is to come down the track and complete a full circle...
-
Write each expression with a rational exponent. (4/2n)
-
Determine the total cost assigned to each job as of APRIL 30 (including the balances from March 31 Marco Company shows the following costs for three jobs worked on in April. Balances on March 31...
-
1. What counsel can you offer long-term investors who are hesitant to invest in stocks and bonds in todays economy? 2. Make a list of the plusses and minuses of investing in either common stock or...
-
Grace is training to be an airplane pilot and must complete five days of flying training in October with at least one day of rest between trainings. How many ways can Grace schedule her flying...
-
The use of null values in a map is problematic, as there is then no way to differentiate whether a null value returned by the call get(k) represents the legitimate value of an entry (k,null), or...
-
A group of children want to play a game, called Unmonopoly, where in each turn the player with the most money must give half of his/her money to the player with the least amount of money. What data...
-
Assuming the input to the sorting problem is given in an array A, describe how to implement the insertion-sort algorithm using only the array A and at most six additional (base-type) variables.
-
Typically, higher coverage ratios are preferred, but too high a ratio may indicate under-utilization of fixed-payment obligations, which may result in unnecessarily low risk and return. O True O False
-
3 points 1 Skipped Chapter 06 Practice Test Question 01 Two years ago, you opened an investment account and deposited $10,000. One year ago, you added another $2,500 to the account. Today, you are...
-
The rights of common stockholders generally include the right to O share proportionately in any new issues of preferred stock. O exclude preferred stockholders from voting rights. O receive cash...
Study smarter with the SolutionInn App