Question

create a deck of cards in netbeans and shuffle it and deal four cards to the...

create a deck of cards in netbeans and shuffle it and deal four cards to the user and display there hand to them.

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

Ans:

public class Deck {
    public static void main(String[] args) {
        String[] SUITS = {
            "Clubs", "Diamonds", "Hearts", "Spades"
        };

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

        // initialize deck
        int n = SUITS.length * RANKS.length;
        String[] deck = new String[n];
        for (int i = 0; i < RANKS.length; i++) {
            for (int j = 0; j < SUITS.length; j++) {
                deck[SUITS.length*i + j] = RANKS[i] + " of " + SUITS[j];
            }
        }

        // shuffle
        for (int i = 0; i < n; i++) {
            int r = i + (int) (Math.random() * (n-i));
            String temp = deck[r];
            deck[r] = deck[i];
            deck[i] = temp;
        }

        // print shuffled deck of four cards
        for (int i = 0; i =< 3; i++) {
            System.out.println(deck[i]);
        }
    }

}
Add a comment
Know the answer?
Add Answer to:
create a deck of cards in netbeans and shuffle it and deal four cards to the...
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
  • Exercise 2.8. We shuffle a deck of cards and deal three cards (without replacement). Find the...

    Exercise 2.8. We shuffle a deck of cards and deal three cards (without replacement). Find the probability that the first card is a queen, the second is a king and the third is an ace.

  • Programming Langaue(Python 3) Define a function deal that will shuffle and distribute the 52 playing cards...

    Programming Langaue(Python 3) Define a function deal that will shuffle and distribute the 52 playing cards evenly to two players (26 each) and return a tuple of each player's hand (as a list of values). The function does not need to take in any arguments, and should create the deck of values internally (i.e., you should not need to input the deck of values into the function; you may reuse the statement you developed for part 6.1). You may assume...

  • Use inheritance and classes to represent a deck of playing cards. Create a Cardclass that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriat...

    Use inheritance and classes to represent a deck of playing cards. Create a Cardclass that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriate accessors, constructors, and mutators. Next, create a Deck class that stores a vector of Card objects. The default constructor should create objects that represent the standard 52 cards and store them in the vector. The Deck class should have functions to: • Print every card in the...

  • *in Java 1. Create a card class with two attributes, suit and rank. 2.In the card...

    *in Java 1. Create a card class with two attributes, suit and rank. 2.In the card class, create several methods: a. createDeck – input what type of deck (bridge or pinochle) is to be created and output an array of either 52 or 48 cards. b.shuffleDeck – input an unshuffled deck array and return a shuffled one. + Create a swap method to help shuffle the deck c. countBridgePoints – inputs a 13-card bridge ‘hand’-array and returns the number of...

  • 3. For a deck consisting of 32 playing cards, let A denote the riffle shuffle which interleaves t...

    3. For a deck consisting of 32 playing cards, let A denote the riffle shuffle which interleaves the top half of the deck with the bottom half, but keeps the top and bottom cards fixed Similarly let B denote the other kind of riffle (in which all cards move). (a) Write out the permutations, of the 32 positions in the deck, represented by these riffles A and B. Give each as a product of cycles b) How many different rearrangements...

  • Now, create a Deck class that consists of 52 cards (each card is an instance of...

    Now, create a Deck class that consists of 52 cards (each card is an instance of class Card) by filling in the template below. Represent the suit of cards as a string: "Spades", "Diamonds", "Hearts", "clubs" and the rank of cards as a single character or integer: 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", class Deck: def init (self): pass # Your code here. def draw (self): Returns the card at the top of the deck, and...

  • 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...

  • 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...

  • 13. [3] You shuffle a deck of playing cards and the chance you get a diamond...

    13. [3] You shuffle a deck of playing cards and the chance you get a diamond card is 0.25. You repeat this process 200 times, putting the card back each time, and out of those 200 attempts you get 57 diamond cards. This demonstrates a. The law of large numbers b. The law of averages 14. [3] You shuffle a deck of playing cards and the chance you get a diamond card is 0.25. You repeat this process times, putting...

  • If you take a deck of 2n playing cards, cut it into two stacks of n...

    If you take a deck of 2n playing cards, cut it into two stacks of n and interleave them, you have performed a perfect shuffle. Example: for the six-card deck [1 2 3 4 5 6]T , the result is [4 1 5 2 6 3]T (note that the top card of the second stack goes on top. Write down the matrix A2n for a perfect shuffle of 2n cards when 2n=10 and 2n=12. How many times must you (perfectly)...

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