Question: I need to add a shuffle() constructor which randomizes the deck, and returns no value. I also need to add a dealOneCard() constructor - which

I need to add a shuffle() constructor which randomizes the deck, and returns no value.

I also need to add a dealOneCard() constructor - which returns one card from the deck to the user(returns one card at a time). Specifically, a call to shuffle() followed by 52 calls to dealOneCard() should result in the user being provided all 52 cards of the deck in a random order. If the caller then makes a 53rd call dealOneCard(), no card is dealt.

Probaly needs to be another class as well.

public class Cards {

public static void main(String[] args)

{

int[] deck = new int[52];

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

String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10","Ace", "Jack", "Queen", "King" };

for (int i = 0; i < deck.length; i++)

deck[i] = i;

for (int i = 0; i < deck.length; i++)

{

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

int temp = deck[i];

deck[i] = deck[shuffle];

deck[shuffle] = temp;

}

for (int i = 0; i < 52; i++)

{

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

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

System.out.println("card number " + deck[i]+": "+rank + " of " + suit);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!