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.

```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&lt;13; i++)
{
CardValue value = CardValue.values()[i];

for (int j=0; j&lt;4; j++)
{
Card card = new Card(value, Suit.values()[j]);
}
}

Collections.shuffle(deck);

Iterator cardIterator = deck.iterator();
while (cardIterator.hasNext())
{
Card aCard = cardIterator.next();
System.out.println(aCard.getCardValue() + " of " + aCard.getSuit());
}
}
}```

#### Earn Coins

Coins can be redeemed for fabulous gifts.