Question

Java programing Write a program that deals a deck of card into 4 hands – Each...

Java programing

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

  • The program should be doing 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

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

25 9 10 11 5 35 47 36 29 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code


import java.util.*;
public class Card {
private int []Hand1;
private int []Hand2;
private int []Hand3;
private int []Hand4;
private ArrayList<Integer> deck=new ArrayList<>();
public Card()
{
Hand1=new int[13];
Hand2=new int[13];
Hand3=new int[13];
Hand4=new int[13];
}
public void createHands()
{
createHand(Hand1);
createHand(Hand2);
createHand(Hand3);
createHand(Hand4);
}
  
public void printFaceValues()
{
System.out.println("Hand 1:");
HandFaceValue(Hand1);
System.out.println("Hand 2:");
HandFaceValue(Hand2);
System.out.println("Hand 3:");
HandFaceValue(Hand3);
System.out.println("Hand 4:");
HandFaceValue(Hand4);
}
public void printHands()
{
System.out.println("Deck of cards shuffled into 4 hands:");
for(int i=0;i<Hand1.length;i++)
System.out.print(Hand1[i]+" ");
System.out.println("");
for(int i=0;i<Hand1.length;i++)
System.out.print(Hand2[i]+" ");
System.out.println("");
for(int i=0;i<Hand1.length;i++)
System.out.print(Hand3[i]+" ");
System.out.println("");
for(int i=0;i<Hand1.length;i++)
System.out.print(Hand4[i]+" ");
}
public static void main(String[] args)
{
Card card=new Card();
card.createHands();
card.printHands();
System.out.println("\nFace of cards in each Hand");
card.printFaceValues();
}

private void createHand(int[] Hand)
{
Random rand = new Random();
for(int i=0;i<Hand.length;i++)
{
int r = rand.nextInt(52)+1;
while(deck.contains(r))
{
r = rand.nextInt(52)+1;
}
deck.add(r);
Hand[i]=r;
}
}

private void HandFaceValue(int[] Hand)
{
String faceValues[]={"1","2","3","4","5","6","7","8","9","10","J","Q","K"};
String clubs="Clubs: ";
String diamonds="Diamonds: ";
String spades="Spades: ";
String heart="Heart: ";
for(int i=0;i<Hand.length;i++)
{
if(Hand[i]>=1 && Hand[i]<=13)
{
clubs+=faceValues[Hand[i]-1]+" ";
}
else if(Hand[i]>=14 && Hand[i]<=26)
{
if(Hand[i]%13==0)
diamonds+=faceValues[12]+" ";
else
diamonds+=faceValues[(Hand[i]%13)-1]+" ";
}
else if(Hand[i]>=27 && Hand[i]<=39)
{
if(Hand[i]%13==0)
spades+=faceValues[12]+" ";
else
spades+=faceValues[(Hand[i]%13)-1]+" ";
}
else
{
if(Hand[i]%13==0)
heart+=faceValues[12]+" ";
else
heart+=faceValues[(Hand[i]%13)-1]+" ";
}
}
System.out.println("\t"+clubs);
System.out.println("\t"+diamonds);
System.out.println("\t"+spades);
System.out.println("\t"+heart);
}
  
}
output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Java programing Write a program that deals a deck of card into 4 hands – Each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • bblem deals with playing cards. The Card API is given below: public class Card ( suit...

    bblem deals with playing cards. The Card API is given below: public class Card ( suit is "Clubs", "Diamonds", "Bearts", or "Spades" Gene=ination s 2", , "10" יי", ,"פ" ,"8" ,"ר" , "6" ,"5י ,-4" ,"ני- * or "A * value is the value of the card number if the card denominat, *is between 2 and 10; 11 for J, 12 for Q, 13 for K, 14 for A public Card (String suit, string denomination){} 1/returns the suit (Clubs, Diamonds,...

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! !!!!!!!!!!!!!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!!!!!!!!!!!!!!!!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in new deck order. (This is the order of a deck of cards new from the box.) The 2 Array lists...

  • Before each draw the deck is well shuffled and a single card randomly drawn. (Use 4...

    Before each draw the deck is well shuffled and a single card randomly drawn. (Use 4 decimals for all answers) A. What is the probability that the first card drawn is a face card (a Jack, a Queen, or a King)? B. What is the probability that the second card drawn is red? C. What is the probability that the first card drawn is a face-card AND the second card drawn is red? D. What is the probability that the...

  • 4. A group of students are playing a card game. The game uses a well-shuffled deck...

    4. A group of students are playing a card game. The game uses a well-shuffled deck of 56 playing cards. 52 of the cards are exactly as described in your textbook. However, there are also 4 Jokers. This means that there are 56 cards: 14 are hearts, 14 are diamonds, 14 are clubs, and 14 are spades. A hand of cards consists of Eight of the cards. Find the number of different hands that contain: a. At least 6 Diamonds....

  • Discrete Mathematics: Counting Principles a. How many hands consists of a pair of aces? b. How...

    Discrete Mathematics: Counting Principles a. How many hands consists of a pair of aces? b. How many hands contain all face cards? c. How many hands contain at least one face card? Concern a hand consisting of 1 card drawn from a standard 52-card deck with flowers on the back and 1 card drawn from a standard 52-card deck with birds on the back. A standard deck has 13 cards from each of 4 suits (clubs, diamonds, hearts, spades). The...

  • C Programming The following code creates a deck of cards, shuffles it, and deals to players....

    C Programming The following code creates a deck of cards, shuffles it, and deals to players. This program receives command line input [1-13] for number of players and command line input [1-13] for number of cards. Please modify this code to deal cards in a poker game. Command line input must accept [2-10] players and [5] cards only per player. Please validate input. Then, display the sorted hands, and then display the sorted hands - labeling each hand with its...

  • Write a class named Card which will represent a card from a deck of cards. A...

    Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that randomly shuffles the cards in the...

  • I am having problem understanding this problem. please explain it explicitly. its a discrete comp...

    I am having problem understanding this problem. please explain it explicitly. its a discrete computer science problem. thanks Exercises 27-32 concern a 5-card hand from a standard 52-card deck. A standard deck has 13 cards from each of 4 suits (clubs, diamonds, hearts, spades). The 13 cards have face value 2 through10, jack, queen, king, or ace Each face value is a "kind" of card. The jack, queen, and king are "face cards. 27. How many hands contain 4 queens?...

  • 2.2.21. Let A be the set of five-card hands dealt from a 52-card poker deck, where...

    2.2.21. Let A be the set of five-card hands dealt from a 52-card poker deck, where the denominations of the five cards are all consecutive—for example, (7 of hearts, 8 of spades, 9 of spades, 10 of hearts, jack of diamonds). Let Bbe the set of five-card hands where the suits of the five cards are all the same. How many outcomes are in the event A ∩ B? the final answer is 40

  • 7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card...

    7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains a pair. b) Determinewhetherthehandcontainstwopairs. c) Determine whether the hand contains three of a kind (e.g., three jacks). d) Determinewhetherthehandcontainsfourofakind(e.g.,fouraces). e) Determine whether the hand contains a flush (i.e., all five cards of the same suit). f) Determine whether the hand contains a straight (i.e., five cards of...

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
ADVERTISEMENT
ADVERTISEMENT