Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You must comment each change with the Step # and what the code will do. IN JAVA import java.util.Arrays; public class CardDeckApp { public static

You must comment each change with the Step # and what the code will do.

IN JAVA

image text in transcribed

image text in transcribed

import java.util.Arrays;

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[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"}; String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

String[] deck = new String[52]; int i = 0; for (String suit : suits) { for (String rank : ranks) { deck[i] = rank + " of " + suit; i++; } } return deck; }

private static void displayCards(String[] cards) { System.out.print("|"); for (String card : cards) { System.out.print(card + "|"); } System.out.println(); }

private static void shuffleDeck(String[] deck) { for (int i = 0; i

private static String[] dealCards(String[] deck, int count) { String[] hand = Arrays.copyOfRange(deck, 0, count); return hand; } }

Exercise 12-1 Use an array list This exercise has you modify the Card Deck application of exercise 11-4 so it uses array lists instead of arrays for the deck of cards and the hands. DECK Ace of Spades | 2 of Spades | 3 of Spades| 4 of Spades- SHUFFLED DECK King of Hearts Jack of Clubs King of Diamonds | 9 of Hearts. FOUR HANDS OF 2 CARDS King of Hearts King of Diamonds Jack of clubs3 of Spades 9 of Hearts | 5 of Hearts 8 of Clubs Jack of Hearts CARDS LEFT IN DECK: 44 Open the project and add the required import statement 1. Open the project named ch 12_ex1_CardDeck in the ex_starts directory. Then, review the code for this application and run it to refresh your memory on how it works. 2. Add an import statement to the Card DeckApp file that makes the ArrayList class available. Modify the application to use array lists 3. Modify the declaration for the getDeck() method so it returns an array list of strings instead of an array of strings. Then, modify the code in this method so it stores the cards in an array list instead of an array. When you declare the array list, set its capacity to 52 since that's the maximum number of cards. 4. Modify the statement in the main() method that calls the getDeck() method so it stores the deck in an array list instead of an array. 5. Modify the displayCards() method so it accepts an array list instead of an array. Since the code for this method uses an enhanced for loop, no additional changes are needed. 6. Modify the shuffleDeck() method so it accepts an array list instead of an array. Then, modify the code for this method so it uses the array list. 7. Modify the dealCards() method so it accepts and returns an array list instead of an array. Then, modify the code for this method so it gets and removes the specified number of cards from the deck and adds them to the hand. 8. Delete the import statement for the Arrays class since it's no longer needed. Then, modify the statement in the main() method that calls the dealCards() method so it stores the hand in an array list instead of an array. Test the application to be sure it works like it did before. Display the number of cards left in the deck and deal multiple hands 9. To be sure that dealt cards are removed from the deck, add a statement that displays the number of cards left in the deck. Then, test this change. 10. Add code to the main() method that deals and displays four hands instead of a single hand. To do that, you can use a loop. Be sure that the statement that displays the number of cards left in the deck is coded outside the loop. Test the application again

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

More Books

Students also viewed these Databases questions

Question

Explain how the topic will affect you in the workplace.

Answered: 1 week ago

Question

Write the difference between sexual and asexual reproduction.

Answered: 1 week ago

Question

What your favourite topic in mathematics?

Answered: 1 week ago

Question

Briefly describe vegetative reproduction in plants.

Answered: 1 week ago