Question: I need help writing this program code in Java. Virtual Playing Card Shuffler For this project, you will be writing a program that is able
I need help writing this program code in Java.
Virtual Playing Card Shuffler
For this project, you will be writing a program that is able to create fresh decks of playing cards, as well as shuffle them and deal them into hands (sets of drawn cards). The methods you will be creating can be used again for future projects (e.g. to create card games). Please follow the directions carefully, as this is a very specific implementation.
Here are the methods you will create:
-
(6 pts) Write a static method called newDeck which returns a String array and takes no parameters. The returned array should have a length of 52 and contain copies of the four suit words (Clubs, Diamonds, Hearts, Spades), indexed in the following manner:
Elements 0-9 and 40-42 of your array should each contain the word Clubs Elements 10-19 and 43-45 should contain Diamonds Elements 20-29 and 46-48 should contain Hearts Elements 30-39 and 49-51 should contain Spades
This array is the basic representation of your deck of cards. Each card will be identified using the array index numbers and the modulo operation (refer to the methods below to see how this is done).
-
(8 pts) Write a static method called draw which returns a String and takes two parameters, one of type String array (the deck being used) and the other of type int (the index of the card being drawn). The returned String will be the textual representation of the card drawn, e.g. A of Spades, 7 of Hearts, or J of Clubs. (Note: FYI, in traditional playing cards, J is for Jack, Q is for Queen, K is for King, A is for Ace).
Build your Strings using the following rules:
-
The suit of your card is the word stored in the array at the given index.
-
If the index is 0, 10, 20, or 30, the card (rank) is an Ace (A).
-
If the index is 40, 43, 46, or 49, the card is a Jack (J).
-
If the index is 41, 44, 47, or 50, the card is a Queen (Q).
-
if the index is 42, 45, 48, or 51, the card is a King (K).
-
For all the other cards, the rank is the remainder of the index divided by 10, plus
one. Example: if the index is 17, the card String will be 8 of Diamonds, since
(17 % 10) + 1 = 8.
-
Important: Before this method returns the card, you should set the current
array element to used, so that you can keep track of which cards have been drawn. See below for more on this.
-
-
(3 pts) Write a static method called printCard that does not have a return value, but takes a String array (the deck) and an int (the index) as parameters. This method simply calls the draw method and displays the result on its own line.
-
(3 pts) Write a static method called printDeck that does not have a return value, but takes a String array (the deck) as a parameter. This method should display the entire deck in the original order by calling the printCard method appropriately (see Sample Output).
-
(5 pts) Write a static method called deal that returns an int and takes a String array (the deck) as a parameter. This method should randomly choose an index (i.e. a card from the deck), and return that index, only after making sure it hasnt been used already. Refer to the last step of number 2 above for a clue on what to do here.
-
(8 pts) Main method: Inside your main program (main method), you should use the static methods you created from above to accomplish the required output (see Sample Output). Your code should be relatively efficient, i.e. use loops and conditions appropriately to accomplish your tasks.
Required output (refer to Sample Output for clarification):
-
After initially creating a new deck, display the original order of the deck with an
appropriate label.
-
After refreshing the deck (call newDeck again), display the entire deck in shuffled order
with an appropriate label.
-
After refreshing the deck again, display the entire deck in a second shuffled order with
an appropriate label.
-
Finally, after refreshing the deck once again, display five random hands (sets) of cards
from the same deck.
-
Important: For each of the above steps, check to make sure there are no repeating
cards in the output. If your shuffling algorithm is working properly, this should simulate drawing from a real deck of cards, which has only one of each card.
-


Shuffie 1 7 of Spades A of Clubs of Diamonds Ozeiginal Order of Deck: 3 of Hearts A of Clubs of Diamonds 6 of Spades A of Spades 4 of Clubs 3 of Diamonds 9 of Clubs 2 of Diamonds 3 of Diamonds 2 of Spades of c2ubs 3of Clubs of Clubs s of Spades ot Clubs 0 ot clubs 9 of Clubs 10 of Clubs A of Diamonds 2 of Diamonds 3 of Diamonds 4 of Diamonds s ot Diamonds e of Diamonds 7 of Diamonds of Spades of Damonds of Clubs of Spades of Diamonds s of Hearts of Spades of Clubs O of Hearts 0 of Spades 0 of Diamonds A of Hearts A of Clubs 0 of Spades K ot Clubs 7 of Clubs K of Diamonds 8 of Diamonds 10 of Clubs 9 of Diamonds 10 of Diamonds of Spades 0 of Ciubs 2 ot Heazts 3 ot Hearts A of Hearts 6 of Diamonds 3 of clubs 0 of Spades 3 of Hearts 3 of Spades of Diamonds 2 of Diamonds 10 of Diamonds of Clubs of Diamonds 2 of Clubs K of clubs of Diamonds 2 of Spades 3 of Spades 4 of Spades 5 of Spades of Spades K of Hearts Q of Spades of Spades 3 of Hearts ot Heazts 4 of Hearts 5 of Dsamonds of Clubs A of Spades 5 of Hearts 2 of Hearts 0 of Hearts K of Spades K of Diamonds 8 of Spades 10 of Spades J of clubs Q of clubs K of Clubs J of Diamonds of Clubs 7 of Di amonds 10 of Clubs y of Di amonds 10 of Hearts of Diamonds K of Di amonds of Spades 7 of Hearts 3 of Diamonds K of Hearts A of Diamonds of Spades of Diamonds 2 of Spades 5 of Spades ( Sample Output (continued): Hand 1: K of Clubs, of Hearts, 2 of Hearts, ot Hearts, A of Clubs Hand 2: K of Diamonds, 2 of Spades, 10 of Hearts, of Diamonds, K of Spades Hand 3: J of Spades, 4 of Clubs, of Clubs, 3 of Hearts, A of Spades Hand 4: 2 of Clubs, 10 of Spades, of Clubs, Q of Clubs, Q of Spades Hand : Q of Diamonds, of Hearts, 7 of Clubs, 3 of Spades, 3 of Clubs BUILD SUCCESSFUL (Cotal eime: seconds)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
