Java Program* NB: Program required to be Array, not ArrayList. Write a program t
ID: 3704480 • Letter: J
Question
Java Program*
NB: Program required to be Array, not ArrayList.
Write a program that reads in a hand of cards and stores it in an array of sets, one set for each suit. Each card is represented using a pair of characters. The first character represents the rank of the card: the digits 2 through 9 stand for themselves, and the letters T, J, Q, K, A stand for ten, jack, queen, king, and ace, respectively. The second character denotes the suit: C, D, H, or S. Check for invalid cards and duplicate cards.
Explanation / Answer
// Class CardInHand definition
public class CardInHand
{
// main method definition
public static void main(String[] args)
{
final int numberOfPerson = 2;
final int numberOfCards = 2;
// Creates a string array for Suits
String[] SUITS = { "Clubs", "Diamonds", "Hearts", "Spades" };
// Creates a string array for Rank
String[] RANKS = { "2", "3", "4", "5", "6", "7", "8", "9", "Ten", "Jack", "Queen", "King", "Ace" };
// Creates a matrix for 2 persons having 2 cards
String[][] deck = new String[numberOfPerson][numberOfCards];
// Assigns cards randomly to each person
// Loops till number of persons
for(int person = 0; person < numberOfPerson; person++)
{
// Loops till number of cards
for(int card = 0; card < numberOfCards; card++)
{
// Generates random number for Ranks
int p = (int) (Math.random() * RANKS.length);
// Generates random number for Suits
int q = (int) (Math.random() * SUITS.length);
// Assigns card to person
deck[person][card] = RANKS[p] + " of " + SUITS[q];
}// End of inner for loop
}// End of outer for loop
// Display each person cards in hand
// Loops till number of persons
for (int person = 0; person < numberOfPerson; person++)
{
// Loops till number of cards
for(int card = 0; card < numberOfCards; card++)
// Display each person each card information
System.out.printf(" Person %d having Card - %d: %s ", (person + 1), (card + 1), deck[person][card]);
}// End of inner for loop
}// End of outer for loop
}// End of main function
Sample Output:
Person 1 having
Card - 1: 9 of Clubs
Person 1 having
Card - 2: King of Spades
Person 2 having
Card - 1: Queen of Diamonds
Person 2 having
Card - 2: Queen of Clubs
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.