Problem

Will the program pick four random cards if you replace lines 22–27 in Listing 1 DeckOfCard...

Will the program pick four random cards if you replace lines 22–27 in Listing 1 DeckOfCards.java with the following code?

for (int i = 0; i<4; i++) {

int cardNumber = (int)(Math.random() * deck.length);

String suit = suits[cardNumber / 13];

String rank = ranks[cardNumber % 13];

System.out.println("Card number " + cardNumber + ": "

+ rank + " of " + suit);

}

LISTING 1 DeckOfCards.java

1 public class DeckOfCards {

2 public static void main(String[] args) {

3 int[] deck = new int[52];

4 String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};

5 String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",

6 "10", "Jack", "Queen", "King"};

7

8 // Initialize the cards

9 for (int i = 0; i

10 deck[i] = i;

11

12 // Shuffle the cards

13 for (int i = 0; i

14 // Generate an index randomly

15 int index = (int)(Math.random() * deck.length);

16 int temp = deck[i];

17 deck[i] = deck[index];

18 deck[index] = temp;

19 }

20

21 // Display the first four cards

22 for (int i = 0; i<4; i++) {

23 String suit = suits[deck[i] / 13];

24 String rank = ranks[deck[i] % 13];

25 System.out.println("Card number " + deck[i] + ": "

26 + rank + " of " + suit);

27 }

28 }

29 }

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT