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.
-
An egg, which for the purposes of this problem can be assumed to be a 5-cm-diameter sphere having the thermal properties of water, is initially at a temperature of 4?C. It is immersed in boiling...
-
A surface condenser is a (a) water tube device (b) steam tube device (c) steam and cooling water mix to give the condensate (d) all of the above
-
On July 1, 2011, Flashlight Corporation sold equipment it had recently purchased to an unaffiliated company for $480,000. The equipment had a book value on Flashlights books of $390,000 and a...
-
Applied vs. Actual Manufacturing Overhead Davis Manufacturing Corporation applies manufacturing overhead on the basis of 150% of direct labor cost. An analysis of the related accounts and job order...
-
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....
-
A student makes the following comment: I can understand why a perfectly competitive firm won't earn a profit in the long run because it charges a price equal to marginal cost. But a monopolistically...
-
Trade secrets do not have to be novel, be non-obvious, or have utility. If that is the case, then what is the economic advantage to a trade secret?
-
Assume that the cost of a particular basket of goods is equal to $108 in the United States and 14,000 in Japan. a. What should the U/$ exchange rate be according to absolute purchasing power parity?...
-
Despite IP representing an intangible asset, several approaches exist to valuation: market, cost-based, and income-based methods are common. Compare and contrast the methods and suggest reasons why...
-
Is "goodwill" a legitimate entry on a balance sheet? Under what conditions does it arise? Provide an example from the current press where goodwill has been enhanced or impaired.
-
There has been a scenario depicted whereby, post-AIA, there will be a preference for trade secrets over patents. Why?
-
As noted in the text, top management of BP adopted a lax approach to safety in its aggressive pursuit of oil discovery. Consider two alternative safety stances it could have adopted. Emulating Exxon...
-
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.
-
In South Africa, the South African Reserve Bank (SARB) maintains liquidity requirements through open market operations. Explain in detail how SARB conducts its Required reserves ratio (10)
-
From the following account balances, prepare in proper form for September (a) an income statement, (b) a statement of owner's equity, and (c) a balance sheet for Fairmont Realty. View the accounts....
-
Sheridan Limited sold equipment on September 30, 2024, for $39,980 cash. The equipment originally cost $145,990 when purchased on January 1, 2022. It had an estimated residual value of $4,330 and a...
Study smarter with the SolutionInn App