Can anyone help me with create this deck? I didn\'t read the directions and crea
ID: 3570078 • Letter: C
Question
Can anyone help me with create this deck? I didn't read the directions and created switch statements instead, but I'm so stuck and I'm desperate for any help. 1500 points to whomever completes this quickest.
Create a new class called Deck that contains 2 static methods. The first one converts a card in a suit (for example: an Ace, 5, or Queen, etc.) to a its corresponding integer (1, 5, or 12, etc.). The second method shuffles the cards in a suit.
In the main class, use three string arrays to hold the cards of three suits needed to play the game.
Explanation / Answer
import java.util.Random;
public class Deck {
private Card[] cards;
int i;
Deck()
{
i=51;
cards = new Card[52];
int x=0;
for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
cards[x] = new Card(a,b);
x++;
}
}
}
public Card drawFromDeck()
{
Random generator = new Random();
int index=0;
do {
index = generator.nextInt( 52 );
} while (cards[index] == null);
i--;
Card temp = cards[index];
cards[index]= null;
return temp;
}
public int getTotalCards()
{
return i;
}
}
/*
First we have a constructor which fills our deck will the cards of various suits and ranks. Every time we add a card at x we increment x to put us at a fresh spot.
drawFromDeck() returns a random card from our container. We need to get a random index and return that card. We have a post-test loop that finds an array index (less than 52) that isn't null. When we find one, we return its card and set it to null. Our deck might need to give feedback on how many cards are left, (maybe to determine if there are enough for another round of black jack or if we need to add all the cards back), so we make a variable i that represents the number cards left in the deck. i starts at 51 and is decremented everytime we take a card out. So if i is -1, then we know we're out of cards.
Now we need to write a test for this program. We want to make sure the cards that get drawn really are drawn in a random order. So we have main:
*/
package javacards;
public class Main {
public static void main(String[] args)
{
Deck deck = new Deck();
Card C;
System.out.println( deck.getTotalCards() );
while (deck.getTotalCards()!= 0 )
{
C = deck.drawFromDeck();
System.out.println( C.toString() );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.