Given these three classes of code: Class 1: Card /** * A class of playing cards.
ID: 3751872 • Letter: G
Question
Given these three classes of code:
Class 1: Card
/**
* A class of playing cards.
*
* @author Erin Encinias
*/
public class Card
{
private static final String[] SUIT_DESCRIPTION =
{"Spades", "Hearts", "Diamonds", "Clubs"};
private static final String[] FACE_VALUE_DESCRIPTION =
{ "Ace", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King"};
private int suit;
private int faceValue;
/**
* Create a new card with a given suit and value
*
* @param v the face value of the card (0 - 12)
* @param s the suit of the card (0 - 3)
*/
public Card(int v, int s)
{
faceValue = v;
suit = s;
}
/**
* @return the suit of this card
*/
public int getValue()
{
return faceValue;
}
/**
* @see java.lang.Object#toString()
*/
public String toString()
{
return FACE_VALUE_DESCRIPTION[faceValue] + " of " + SUIT_DESCRIPTION[suit];
}
}
Class 2: Cardrunner
/**
* Just a main method to let us play with Decks
* and Cards
*/
public class CardRunner
{
/**
* Variables of the type Card are declared and cards are created as well
* The deck of the cards is created and shuffled
* @param args
*/
public static void main(String[] args)
{
Card c1 = new Card (0,0);
System.out.println(c1);
Card c2 = new Card(12,3);
System.out.println(c2);
System.out.println();
Deck d = new Deck();
d.shuffle();
System.out.println(d);
}
}
Class 3: Deck
/**
* A standard deck of 52 cards in four suits.
*/
public class Deck
{
private static final int NUMBER_OF_SUITS = 4;
private static final int NUMBER_OF_CARDS = 52;
Card[] cards;
/**
* Create the deck by filling it with the appropriate cards
*/
public Deck()
{
cards = new Card[NUMBER_OF_CARDS];
int position = 0;
for (int suit = 0; suit < NUMBER_OF_SUITS; suit++)
{
for (int faceValue = 0;
faceValue < NUMBER_OF_CARDS/NUMBER_OF_SUITS;
faceValue++)
{
cards[position] =
new Card(faceValue, suit);
position++;
}
}
}
/**
* @see java.lang.Object#toString()
*/
public String toString()
{
String s = new String();
for (int i = 0; i < cards.length; i++)
{
s = s + cards[i] + " ";
}
return s;
}
/**
* randomly shuffle the cards using the Fisher-Yates shuffle
*/
public void shuffle()
{
for (int i = 0; i < NUMBER_OF_CARDS; i++)
{
int swapPosition = (int) (Math.random() * (NUMBER_OF_CARDS - i) + i);
Card temp = cards[i];
cards[i] = cards[swapPosition];
cards[swapPosition] = temp;
}
}
}
Question:
What was the original order in which the Deck was created?
Explanation / Answer
Note: I just added few line of code in CodeRunner.java file to display the actual order of cards .Later dislayed deck of cards after shuffling.Could u tell me if u have any doubts in this.Thank You
_______________
CardRunner.java
/**
* Just a main method to let us play with Decks
* and Cards
*/
public class CardRunner
{
/**
* Variables of the type Card are declared and cards are created as well
* The deck of the cards is created and shuffled
* @param args
*/
public static void main(String[] args)
{
Card c1 = new Card (0,0);
System.out.println(c1);
Card c2 = new Card(12,3);
System.out.println(c2);
System.out.println();
Deck d = new Deck();
System.out.println("Displaying Deck of Cards :");
System.out.println(d);
d.shuffle();
System.out.println(" Displaying Deck of Cards after shuffling:");
System.out.println(d);
}
}
________________
Card.java
/**
* A class of playing cards.
*
* @author Erin Encinias
*/
public class Card
{
private static final String[] SUIT_DESCRIPTION =
{"Spades", "Hearts", "Diamonds", "Clubs"};
private static final String[] FACE_VALUE_DESCRIPTION =
{ "Ace", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King"};
private int suit;
private int faceValue;
/**
* Create a new card with a given suit and value
*
* @param v the face value of the card (0 - 12)
* @param s the suit of the card (0 - 3)
*/
public Card(int v, int s)
{
faceValue = v;
suit = s;
}
/**
* @return the suit of this card
*/
public int getValue()
{
return faceValue;
}
/**
* @see java.lang.Object#toString()
*/
public String toString()
{
return FACE_VALUE_DESCRIPTION[faceValue] + " of " + SUIT_DESCRIPTION[suit];
}
}
________________
Deck.java
/**
* A standard deck of 52 cards in four suits.
*/
public class Deck
{
private static final int NUMBER_OF_SUITS = 4;
private static final int NUMBER_OF_CARDS = 52;
Card[] cards;
/**
* Create the deck by filling it with the appropriate cards
*/
public Deck()
{
cards = new Card[NUMBER_OF_CARDS];
int position = 0;
for (int suit = 0; suit < NUMBER_OF_SUITS; suit++)
{
for (int faceValue = 0;
faceValue < NUMBER_OF_CARDS/NUMBER_OF_SUITS;
faceValue++)
{
cards[position] =
new Card(faceValue, suit);
position++;
}
}
}
/**
* @see java.lang.Object#toString()
*/
public String toString()
{
String s = new String();
for (int i = 0; i < cards.length; i++)
{
s = s + cards[i] + " ";
}
return s;
}
/**
* randomly shuffle the cards using the Fisher-Yates shuffle
*/
public void shuffle()
{
for (int i = 0; i < NUMBER_OF_CARDS; i++)
{
int swapPosition = (int) (Math.random() * (NUMBER_OF_CARDS - i) + i);
Card temp = cards[i];
cards[i] = cards[swapPosition];
cards[swapPosition] = temp;
}
}
}
__________________
Output:
Ace of Spades
King of Clubs
Displaying Deck of Cards :
Ace of Spades
2 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
Ace of Hearts
2 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
2 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
2 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
Displaying Deck of Cards after shuffling:
Queen of Clubs
Jack of Clubs
Queen of Diamonds
Ace of Diamonds
5 of Hearts
5 of Clubs
Ace of Hearts
6 of Hearts
Ace of Spades
8 of Diamonds
9 of Spades
8 of Hearts
8 of Clubs
10 of Diamonds
7 of Spades
3 of Diamonds
7 of Diamonds
Jack of Hearts
Ace of Clubs
King of Diamonds
10 of Clubs
7 of Hearts
Jack of Diamonds
2 of Spades
6 of Clubs
10 of Spades
King of Hearts
4 of Clubs
3 of Spades
3 of Hearts
Queen of Spades
King of Clubs
9 of Diamonds
10 of Hearts
2 of Clubs
8 of Spades
4 of Diamonds
King of Spades
Queen of Hearts
6 of Diamonds
4 of Hearts
2 of Hearts
9 of Clubs
2 of Diamonds
6 of Spades
4 of Spades
5 of Diamonds
3 of Clubs
Jack of Spades
7 of Clubs
5 of Spades
9 of Hearts
_________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.