In: Computer Science
Using Java, design a program that creates an array of
Card objects, shuffles them, then recursively sorts them by value
and then recursively sorts them by value and suit
You will need to create a Card class with the following two
fields:
• value (a String)
• suit (a String)
You may include any other fields, constructors, or methods as
needed.
Your single array will need to contain 52 card objects, one for
each value of each suit. The values to use are 2-10, J, Q, K, and
A, with A being the card with the highest value. The suit values to
use are H (for Hearts), D (for Diamonds), C (for Clubs), and S (for
Spades).
After building the array, shuffle the array using the Fisher-Yates
Algorithm. You must implement this algorithm yourself. After
shuffling, print each object’s value and suit to show the array was
shuffled. The output for each object printed should be VALUE -
SUIT. For example: 2 - H.
Then, you’ll need to recursively sort the array based on the
values. All 2 cards should be at the beginning and all A cards
should be at the end; The suit is irrelevant here. After sorting,
print each object’s value and suit again to show the array was
sorted based on the cards’ values.Finally, you’ll recursively sort
the array based on the values and suit. Clubs should be first, then
Diamonds, then Hearts, then Spades. Each suit should be sorted from
2-A. After sorting, print each object’s value and suit again to
show the array was sorted based on the cards’ values and
suits.
please comment
Thanks for the question, here are the two classes - Card and Deck. Deck is the driver class, Deck class create an array fof cards, shuffles the cards using the Fisher-Yates algorithm and then recursively sorts the array using bubble sort alogrithm . It prints both the shuffled and sorted cards.
Here you go !
===========================================================
public class Card
implements Comparable<Card> {
private String
value;
private String
suit;
public Card(String val, String
suit) {
value =
val;
this.suit = suit;
}
public String getSuit() {
return
suit;
}
public String getValue()
{
return
value;
}
@Override
public String toString()
{
return
getValue() + " - " + getSuit();
}
@Override
public int compareTo(Card card)
{
return
cardRanking(this) - cardRanking(card);
}
// returns the card value club 2 has ranking 1 while card
Spades - A has value 52
private static int
cardRanking(Card card) {
String cards[] =
{"2", "3", "4",
"5", "6", "7",
"8", "9", "10",
"J", "Q", "K",
"A"};
String suits[] =
{"C", "D", "H",
"S"};
int
rank = 0;
for
(int i = 0; i < cards.length;
i++) {
if (cards[i].equals(card.getValue())) {
rank = i + 1;
break;
}
}
int
suitRank = 0;
for
(int i = 0; i < suits.length;
i++) {
if
(suits[i].equals(card.getSuit())) {
suitRank = i+1;
break;
}
}
return
(suitRank * 13) + rank;
}
}
=============================================================
import java.util.Random;
public class Deck {
public static void
main(String[] args) {
String values[] =
{"2", "3", "4",
"5", "6", "7",
"8", "9", "10",
"J", "Q", "K",
"A"};
String suits[] =
{"C", "D", "H",
"S"};
// create an array of Card object
Card deckOfCard[] =
new Card[values.length *
suits.length];
int
index = 0;
for
(String value : values) {
for (String suit : suits) {
deckOfCard[index++] = new Card(value, suit);
}
}
shuffle(deckOfCard);
System.out.println("Printing the shuffled
deck of cards - ");
displayDeckOfCards(deckOfCard);
recusriveBubbleSort(deckOfCard,deckOfCard.length);
System.out.println("Printing the sorted
deck of cards - ");
displayDeckOfCards(deckOfCard);
}
private static void
displayDeckOfCards(Card[] deckOfCard) {
for
(Card card : deckOfCard) {
System.out.println(card);
}
}
private static void
shuffle(Card[] cards) {
// shuffle using fisher
yates algorithm
Random random =
new Random();
for
(int i = 0; i < cards.length -
1; i++) {
int j = i +
random.nextInt(cards.length - i);
Card temp = cards[i];
cards[i] = cards[j];
cards[j] = temp;
}
}
// recursively sorts the array of cards
private static void
recusriveBubbleSort(Card cards[], int n) {
if (n
== 1) return;
for
(int i = 0; i < n - 1; i++) {
if (cards[i].compareTo(cards[i + 1]) >
0){
Card temp = cards[i];
cards[i] = cards[i + 1];
cards[i + 1] =
temp;
}
}
recusriveBubbleSort(cards,n-1);
}
}
========================================================