Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USING JAVA MODIFY THE CODE FOLLOWING THESE STEPS: Part 1: Build the Deck Add code to the getDeck() method so it returns a standard deck

USING JAVA MODIFY THE CODE FOLLOWING THESE STEPS:

Part 1: Build the Deck

Add code to the getDeck() method so it returns a standard deck of 52 cards.

To do that:

Create one array of strings for the four suits (Spades, Hearts, Clubs, Diamonds) and a second array of strings for the 13 ranks for each suit. You can hard code the values using the curly braces method.

Then create a nested loop to loop through the suits array and inside loop through the ranks array. All while concatenating the current suit with current rand and putting it into a String array. This is the array that you will return. Note that it is NOT a 2D array. We are just using nested loops to help build a single array not a 2D Array.

HINTS: You will need a variable counter that you use to assign each value in the deck array. After each iteration you would need to increase it (counter++) so that you can assign the next value.

image text in transcribed

Part 2: Display the Deck

Add code to the displayCards() method so it prints all cards in the array that's passed to it.

Use a simple for loop to print out the contents of the array, separate each card with a pipe character ( | ).

Once this method is written you should be able to test your code and see that the deck of cards prints out. If it doesn't match the first part of the output from the above image then you may have to not only check your code for this part but revisit part 1 to make sure that is done correctly.

Part 3: Shuffle the Cards

Add code to the shuffleDeck() method that shuffles the deck of cards.

You will want to loop through the array and swap each card with a card at random

To do this get a random number between 0 - the size of the array that you then swap the values.

to do a swap remember that you need to save the old value of one of the elements to a temporary variable to then perform the saw and assign the temp variable back to the array element

image text in transcribed

Part 4: Deal the Cards

Add code to the dealCards() method that creates a hand of cards by dealing the specified number of cards from the cards array. Notes this value is sent to the method as an argument.

Take the first cards in the deck up until the sent count amount

You will need to return a String array of the hand dealt.

Notice the hand array is created for you based on the count variable size

You can use a loop to assign values but you can also use the Array class to help you copy it.

Part 5: Check your Program

Lastly you should run the program and the final output should be similar to the image provided above. The only difference is that the SHUFFLED DECK part should be different each time. Also make sure that the HAND OF 2 CARDS matches the first two cards from SHUFFLED DECK and is not blank.

image text in transcribed

THE CODE TO MODIFY IS BELOW:

public class CardDeckApp { public static void main(String[] args) { System.out.println("DECK"); String[] deck = getDeck(); displayCards(deck); System.out.println("SHUFFLED DECK"); shuffleDeck(deck); displayCards(deck); int count = 2; System.out.println("HAND OF " + count + " CARDS"); String[] hand = dealCards(deck, count); displayCards(hand); } private static String[] getDeck() { String[] deck = new String[52]; // add code that creates deck here return deck; } private static void displayCards(String[] cards) { // add code that displays cards here } private static void shuffleDeck(String[] deck) { int randomIndex = (int) (Math.random() * deck.length-1); // add code that shuffles the deck here } private static String[] dealCards(String[] deck, int count) { String[] hand = new String[count]; return hand; } } 

counter =0; for loop for suits \{ for loop for ranks \{ deck [counter]2=suitsandrankscombinedhere;increasecounterby1 } for loop code here \{ rndNum = code to get a random number temp = array [i];// store old value array [i]= array [ rndNum ];// do a swap with random number spot array [ rndNum ]= temp; // put old value to the random number spot \} |Ace of Spades|2 of Spades|3 of Spades|4 of Spades|5 of Spades|6 of Spades|7 of Spe SHUFFLED DECK |5 of Spades|3 of Spades|4 of Spades|Jack of Diamonds|Ace of Spades|6 of Diamonds|l HAND OF 2 CARDS |5 of Spades|3 of Spades|

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Demystifying Databases A Hands On Guide For Database Management

Authors: Shiva Sukula

1st Edition

8170005345, 978-8170005346

More Books

Students also viewed these Databases questions

Question

How can you provide security in IoT? Explain.

Answered: 1 week ago