Javs Assignment #13 In Assignment #11, you created a deck of cards stored in a s
ID: 3580685 • Letter: J
Question
Javs
Assignment #13
In Assignment #11, you created a deck of cards stored in a single-dimensional array. You will now put that to good use as the basis for this assignment.
Task #1 – Do the Shuffle
Now that you’ve created a “pseudo” deck of cards that you have stored in an array, now programmatically “shuffle” them in order to create a deck of cards that is randomly sorted. How you accomplish this task is up to you! You must use either the Array or ArrayList for this task.
Task #2 – Display both the Unshuffled and Shuffled Decks
Display both decks in a comma delimited format. (i.e., 3 of Hearts, 7 of Clubs, etc. …)
Explanation / Answer
//Card.java
public class Card
{
//declare face and suit of string type
private final String face;
private final String suit;
//Constructor that sets face and suit values of class
public Card(String face, String suit)
{
this.face = face;
this.suit = suit;
}
//Return String representation of Card
public String toString()
{
return face + " of " + suit;
}
} // end class Card
------------------------------------------------------------------------------------------------------------
//eckOfCards.java
//DeckOfCards class represents a deck of playing cards.
import java.security.SecureRandom;
public class DeckOfCards
{
// array of Card objects
private Card[] deck;
// index of next Card to be dealt (0-51)
private int currentCard;
//number of card in deck
private static final int NUMBER_OF_CARDS = 52;
// random number generator
private static final SecureRandom randomNumbers = new SecureRandom();
// constructor fills deck of Cards
public DeckOfCards()
{
String[] faces = {"Ace", "Deuce", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King"};
String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
deck = new Card[NUMBER_OF_CARDS]; // create array of Card objects
currentCard = 0; // first Card dealt will be deck[0]
// populate deck with Card objects
for (int count = 0; count < deck.length; count++)
deck[count] =
new Card(faces[count % 13], suits[count / 13]);
}
//Shuffle deck of Cards with random number in each iteration
public void shuffle()
{
// next call to method dealCard should start at deck[0] again
currentCard = 0;
// for each Card, pick another random Card (0-51) and swap them
for (int first = 0; first < deck.length; first++)
{
// select a random number between 0 and 51
int second = randomNumbers.nextInt(NUMBER_OF_CARDS);
// swap current Card with randomly selected Card
Card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
}
//Returns the string representation of cards
@Override
public String toString()
{
String cards="";
for (Card card : deck) {
cards+=card+" ";
}
return cards;
}
} // end class DeckOfCards
------------------------------------------------------------------------------------------------------------
//Test Card and DeckOfCards classes
//TestDeckShuffle.java
public class TestDeckShuffle
{
public static void main(String[] args) {
//Create an instanace of DeckOfCards class
DeckOfCards unshuffled=new DeckOfCards();
System.out.println("----------------------");
System.out.println("Un-shuffled deck of cards");
System.out.println("----------------------");
//call toString to print unshuffled cards
System.out.println(unshuffled.toString());
//call shuffle method to shuffle the cards in deck
unshuffled.shuffle();
System.out.println("----------------------");
System.out.println("Shuffled deck of cards");
System.out.println("----------------------");
//call toString to print shuffled cards
System.out.println(unshuffled.toString());
}
}
------------------------------------------------------------------------------------------------------------
Sampel output:
----------------------
Un-shuffled deck of cards
----------------------
Ace of Hearts
Deuce of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Diamonds
Deuce of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Clubs
Deuce of Clubs
3 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Spades
Deuce of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
10 of Spades
Jack of Spades
Queen of Spades
King of Spades
----------------------
Shuffled deck of cards
----------------------
3 of Clubs
Ace of Clubs
7 of Hearts
4 of Diamonds
Jack of Clubs
3 of Hearts
5 of Spades
7 of Spades
Queen of Hearts
Deuce of Hearts
4 of Clubs
10 of Diamonds
Jack of Spades
Deuce of Clubs
5 of Hearts
6 of Spades
Queen of Spades
King of Hearts
Queen of Clubs
10 of Hearts
9 of Spades
5 of Clubs
Ace of Diamonds
King of Clubs
Ace of Spades
9 of Clubs
8 of Diamonds
Jack of Diamonds
7 of Clubs
Queen of Diamonds
8 of Hearts
Deuce of Diamonds
Deuce of Spades
10 of Clubs
10 of Spades
4 of Spades
3 of Diamonds
3 of Spades
8 of Clubs
4 of Hearts
5 of Diamonds
6 of Clubs
9 of Diamonds
6 of Hearts
7 of Diamonds
6 of Diamonds
8 of Spades
King of Spades
Jack of Hearts
9 of Hearts
Ace of Hearts
King of Diamonds
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.