Question

JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card...

JAVA Exercise_Pick a Card

Write a program that simulates the action of picking of a card from a deck of 52 cards. Your program will display the rank and suit of the card, such as “King of Diamonds” or “Ten of Clubs”.

You will need:

• To generate a random number between 0 and 51

• To define two String variables:

a. The first String should be defined for the card Suit (“Spades”, “Hearts”, “Diamonds”, “Clubs”)

b. The second String should be defined for the card Rank (“Ace”, “Two”, “Three”, … , “Jack”, “Queen”, “King”)

• To create variables to store the randomly generated number for a card rank number (e.g. r) and a suit number (e.g. s).

• Integer Division to distinguish between the suits in the deck

a. Consider the following ranges of integers to represent the cards:

- 0 to 12 can represent the 13 Spades

- 13 to 25 can represent the 13 Hearts

- 26 to 38 can represent the 13 Diamonds

- 39 to 51 can represent the 13 Clubs

• The modulus operator to distinguish between each rank

a. A remainder of 0 will represent “Ace”, remainder of 1 for “Two” and so on

• Two methods to determine the suit and rank

a. The first method will contain an if else-if statement that assigns a rank to the String variable for rank based on the value of r

- if r is 0, assign “Ace” to the variable

   - Otherwise, if r is 1 assign “Two” to the String variable for rank (and so on)

- This method should accept the int value of r and returns a String that corresponds to the rank.

b. The second method will contain an if else-if that assigns a value to the String variable for suit based on the value of s

   - if s is 0, assign “Spades” to the String variable for suit

- Otherwise, if r is 1 assign “Hearts” to the String variable for Suit (and so on)

- This method should accept the int value of s and returns a String that corresponds to the suit.

c. Important: Only assignment should be done within the if else structures

(NO print or println statements should be within them.)

• Output statements (within the main method) display your results after the selections are made. Your two methods can be called from within your output statements.

Extra Credit : Use enumerations instead of Strings to complete this program. Create two enums to store values of type Rank and the Suit respectively, and modify your methods so that it returns the enum (i.e. Rank, Suit) instead of String.

Homework Answers

Answer #1
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
 
public class Deck
{
  private ArrayList deck;
 
  public Deck ()
  {
    this.deck = new ArrayList();
    for (int i=0; i<13; i++)
    {
      CardValue value = CardValue.values()[i];
 
      for (int j=0; j<4; j++)
      {
        Card card = new Card(value, Suit.values()[j]);
        this.deck.add(card);
      }
    }
 
    Collections.shuffle(deck);
 
    Iterator cardIterator = deck.iterator();
    while (cardIterator.hasNext())
    {
      Card aCard = cardIterator.next();
      System.out.println(aCard.getCardValue() + " of " + aCard.getSuit());
    }
  }
}
Know the answer?
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
Similar Questions
As shown above, a classic deck of cards is made up of 52 cards, 26 are...
As shown above, a classic deck of cards is made up of 52 cards, 26 are black, 26 are red. Each color is split into two suits of 13 cards each (clubs and spades are black and hearts and diamonds are red). Each suit is split into 13 individual cards (Ace, 2-10, Jack, Queen, and King). If you select a card at random, what is the probability of getting: (Round to 4 decimal places where possible) a) A 9 of...
he following question involves a standard deck of 52 playing cards. In such a deck of...
he following question involves a standard deck of 52 playing cards. In such a deck of cards there are four suits of 13 cards each. The four suits are: hearts, diamonds, clubs, and spades. The 26 cards included in hearts and diamonds are red. The 26 cards included in clubs and spades are black. The 13 cards in each suit are: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. This means there are four...
Probabilities with a deck of cards. There are 52 cards in a standard deck of cards....
Probabilities with a deck of cards. There are 52 cards in a standard deck of cards. There are 4 suits (Clubs, Hearts, Diamonds, and Spades) and there are 13 cards in each suit. Clubs/Spades are black, Hearts/Diamonds are red. There are 12 face cards. Face cards are those with a Jack (J), King (K), or Queen (Q) on them. For this question, we will consider the Ace (A) card to be a number card (i.e., number 1). Then for each...
As shown above, a classic deck of cards is made up of 52 cards, 26 are...
As shown above, a classic deck of cards is made up of 52 cards, 26 are black, 26 are red. Each color is split into two suits of 13 cards each (clubs and spades are black and hearts and diamonds are red). Each suit is split into 13 individual cards (Ace, 2-10, Jack, Queen, and King). If you select a card at random, what is the probability of getting: 1) A(n) 8 of Heart s? 2) A Club or Spade?...
The following question involves a standard deck of 52 playing cards. In such a deck of...
The following question involves a standard deck of 52 playing cards. In such a deck of cards there are four suits of 13 cards each. The four suits are: hearts, diamonds, clubs, and spades. The 26 cards included in hearts and diamonds are red. The 26 cards included in clubs and spades are black. The 13 cards in each suit are: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. This means there are four...
The following question involves a standard deck of 52 playing cards. In such a deck of...
The following question involves a standard deck of 52 playing cards. In such a deck of cards there are four suits of 13 cards each. The four suits are: hearts, diamonds, clubs, and spades. The 26 cards included in hearts and diamonds are red. The 26 cards included in clubs and spades are black. The 13 cards in each suit are: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. This means there are four...
We are creating a new card game with a new deck. Unlike the normal deck that...
We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 15. ii) One of 5 different suits. Hence, there are 75 cards in the deck with 15 ranks for each of the 5 different suits, and none of the cards will...
Complete the implementation of the Card class. The two methods that you need to complete are...
Complete the implementation of the Card class. The two methods that you need to complete are compareTo and equals. CopareTo API: Compares this card to another card for order. This method imposes the following order on cards: Cards are first compared by rank. If the rank of this card is less than the rank of the other card then -1 is returned. If the rank of this card is greater than the rank of the other card then 1 is...
Using Java, please implement the Poker and PokerHand classes to provide the expected output using the...
Using Java, please implement the Poker and PokerHand classes to provide the expected output using the CardDemo class. Rules are the following: - Deck and PockerHand should be Iterable (note that you may just reuse one of the underlying iterators to provide this functionality) - Poker should implement 2 methods checking for two possible poker hands , see the comments inside the classes for details. Hint: remember how one can count the frequency of words in text? - whenever you...
Determine the poker odds of drawing the following hand from a standard card deck. (4 suits,...
Determine the poker odds of drawing the following hand from a standard card deck. (4 suits, 13 ranks in each suit.) What are the odds of drawing a royal flush (AKQJ10 all of the same suit)? Drawing cards is WITHOUT replacement. (NOTE: If necessary, lay out 52 cards on a table and do a dry run before computing the probabilities!) In order to get a royal flush in spades, you must pick the following 5 cards: What is the probability...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT