Question

In: Computer Science

Dealing Cards Write a program that deals a deck of card into 4 hands – Each...

Dealing Cards

Write a program that deals a deck of card into 4 hands – Each hand having 13 cards.

  • Your program should do the followings
    • use an array to randomly select 52 cards and deal it into 4 hands.
    • Print each hand unsorted
    • Identify the face value of each hand and display it.
    • You should create a class called Card, and all the methods should be defined within the card class.

Hints – Import below java utility to use the random function.

import java.util.*;

Random rand = new Random();

int r = rand.nextInt(52); // this will generate random numbers between 0-52

to get a value between 1-13, use the mod function and get the remainder

if your number is 15, 15%13 has a remainder of 2. 2 is your face card.

---------------------------------------------------------------------------

11 = jack

12= Queen

13= King

Card is a CLUB - If your generated number is between (1-13)

Card is a Diamond - If your generated number is between (14-26)

Card is a Spade - If your generated number is between (27-39)

Card is a Heart - If your generated number is between (40-52)

Your program output should look like this below

----------------------------------------------------------------------------------------------------------------------------------

Deck of cards shuffled into 4 hands

8 1 7 18 43 26 31 2 12 3 40 22 4

29 9 10 11 5 35 47 36 25 14 17 39 23

21 52 6 46 38 48 24 16 27 32 13 45 42

49 44 50 20 37 34 51 15 28 30 19 33 41

Face of cards in each Hand

Hand 1:

       Clubs: 8 1 7 2 Q 3 4

       Diamonds: 5 K 9

       Spades: 5

       Heart: 4 1

Hand 2:

       Clubs: 4 9 10 J 5

       Diamonds: Q 1 4

       Spades: 3 9 10 K

       Heart: 8

Hand 3:

       Clubs: 6 K

       Diamonds: 10 8 J 3

       Spades: K Q 1 6

       Heart: K 7 9

Hand 4:

       Clubs: K

       Diamonds: 7 2

       Spades: J 8 2 4

Heart: 6 3 10 5 J Q

Solutions

Expert Solution

In case of any query do comment. Please rate answer. Thanks

Please use below code:

=======================Card.java====================

import java.util.*;

public class Card

{

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

    //string array for Ranks, 13 index would be 0 when take modulus so It is K.

    String[] Ranks = {"K", "1","2","3","4","5","6","7","8","9","10","J","Q"};

   

    //constructor to initialize the Card Deck with it's index value

    public Card()

    {

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

        {

            deck[index] = index +1;

        }

    }

   

    //shuffle cards

    public void ShuffleCards()

    {

         

        //shuffle the cards, by generating random number and then shuffle the indexes

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

        {

            Random rand = new Random();

           

            int newIndex = rand.nextInt(52);

           

            int temp = deck[index];

            deck[index] = deck[newIndex];

            deck[newIndex] = temp;

        }

    }

   

    //To print the hands as desired output

    public void PrintHands()

    {

        System.out.println("Deck of cards shuffled into 4 hands");

       

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

        {

            System.out.print(deck[index] + " ");

            if ((index+1) % 13 ==0)

            {

                System.out.println();

            }

        }

       

        System.out.println("Face of cards in each Hand");

       

        //Print the informartion for each hand group by suites

        for (int index =0; index < 4 ; index++ )

        {

            System.out.println("Hand " + (index+1) + ":");

            int startIndex = index *13;

            int endIndex = startIndex+13;

            PrintHandsGroupBySuites(startIndex, endIndex);   

        }

    }

   

    //for each hand, create the information group by suites and then print

    public void PrintHandsGroupBySuites(int startIndex,int endIndex)

    {

       

        

        String[] cardHandInformation = {"Clubs:" , "Diamonds:","Spades:","Heart:"};

       

        for (int index =startIndex ; index<endIndex ; index++ )

        {

            //card suit would be 1-13 (club), 14-26 (Diamond), 27-39(Spades), 40-52(Heart)

            int cardSuit = deck[index]/14;

            int cardRank = deck[index]% 13;

          

            cardHandInformation[cardSuit] += " " + Ranks[cardRank];

        }

       

        //print the information

        for (int index=0; index < cardHandInformation.length ; index ++ )

        {

            System.out.println("\t" + cardHandInformation[index]);

        }

    }

   

}

============================main.java (driver program)================

public class Main

{

              public static void main(String[] args) {

        //create an object of Card class

              Card deckCard = new Card();

              //shuffle the deck

                             deckCard.ShuffleCards();

                             //print Hands

                             deckCard.PrintHands();

              }

}

=======================screen shot of the code===================

Output:


Related Solutions

How many different 4 card hands can be dealt from a deck of 52 cards? The...
How many different 4 card hands can be dealt from a deck of 52 cards? The order of the cards does not matter in this case.
In poker, there is a 52 card deck with 4 cards each of each of 13...
In poker, there is a 52 card deck with 4 cards each of each of 13 face values. A full house is a hand of 5 cards with 3 of one face value, and 2 of another. What is the probability that a random poker hand is a full house? You can leave your answer in terms of bionomial co-efficients and similar factors, but please explain each term.
Consider five-card-hands out of a deck of 52 cards. a) (10%) What is the probability that...
Consider five-card-hands out of a deck of 52 cards. a) (10%) What is the probability that a hand selected at random contains five cards of the same suit? (The suits are hearts, spades, diamonds and clubs.) Show a formula and compute a final answer. Show your intermediate computations. b) (10%) In how many different ways can a hand contain no more than 2 cards of the same kind. (E.g. not more than 2 queens.) Show a formula but you do...
1, Suppose you want to divide a 52 card deck into four hands with 13 cards...
1, Suppose you want to divide a 52 card deck into four hands with 13 cards each. What is the probability that each hand has a king? 2.   Suppose that X ∼ Bin(n, 0.5). Find the probability mass function of Y =2X, E(Y) , and D(Y).
2.Deck of cards(52 total cards; 13 denominations in each of 4 suits). Select a single card...
2.Deck of cards(52 total cards; 13 denominations in each of 4 suits). Select a single card at random from a deck of cards: a.Whatis the probability of selecting the king of hearts? b.What is the probability of selecting a king? c.What is the probability of selecting a heart? d.What is the probability of selecting a king or a heart?
Program a simple game that shuffles a deck of 52 cards and hands them out to...
Program a simple game that shuffles a deck of 52 cards and hands them out to twoplayers randomly (26 each). Write a simulation of the card game, where, at each turn, bothplayers take the card from the top of their pile and the player with the higher card wins thatround. The winner of that round takes both cards and adds it to a stack of won cards.In essence, there are four stacks. Each player has one stack of cards they...
1. How many 5 card poker hands are possible from a standard deck of cards? 2....
1. How many 5 card poker hands are possible from a standard deck of cards? 2. What is the probability of a Royal Flush? 3. What is the probability of a Full House? 4. What is the probability of Two Pair? 5. How many more times likely is Two Pair than a Full House?   
4. The experiment is to draw a card from a standard deck of cards. Consider the...
4. The experiment is to draw a card from a standard deck of cards. Consider the following events: Q = drawing a Queen R = drawing a red card a. Are Q and R mutually exclusive? Clearly explain. b. Find P(Q) and P(Q|R). c. Are the events Q and R independent? Clearly explain.
Write a console application called PlayingCards that has 4 classes: PlayingCards, Deck, Cards, Card. This application...
Write a console application called PlayingCards that has 4 classes: PlayingCards, Deck, Cards, Card. This application will be able to create a deck of cards, shuffle it, sort it and print it. Use the following class diagrams to code your application. Use the following code for the PlayingCards class public class PlayingCards{ public static void main(String[] args){                 System.out.println("Playings Cards");                 Deck deck = new Deck();                 deck.create();                 System.out.println("Sorted cards");                 deck.sort();                 deck.showCards();                 System.out.println("Shuffled cards");                 deck.shuffle();...
A standard deck of cards contains 52 cards. One card is selected from the deck. (a)...
A standard deck of cards contains 52 cards. One card is selected from the deck. (a) Compute the probability of randomly selecting a jack or ace. ​(b) Compute the probability of randomly selecting a jack or ace or nine. ​(c) Compute the probability of randomly selecting a king or diamond.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT