I am trying to implement a method to report the number of cards in a deck after
ID: 3662067 • Letter: I
Question
I am trying to implement a method to report the number of cards in a deck after each card is dealt, here is my code. Currently the cardsInDeck is not being reached and I need to call the method in the driver also. Any help is appreciated!! Thanks.
//DECK OF CARDS
import java.util.Random;
public class DeckOfCards
{
private Card[] deck; //Array to store Card class objects
private int currentCard; //Current card
private static final int NUMBER_OF_CARDS = 52; //Number of cards in the decks
private int cardsInDeck = 52; //Number of cards in the deck at any given time
private static final Random randomNumbers = new Random(); //Random
/** Creates a new instance of DeckOfCards */
public DeckOfCards()
{
deck = new Card[NUMBER_OF_CARDS];
currentCard = 0;
//fills deck with Card class objects
//which has suit and face enumerations
int count = 0;
for(Card.Suit suit : Card.Suit.values())
{
for(Card.Face face : Card.Face.values())
{
deck[count] = new Card(face,suit);
count++;
}
}
}
//Shuffles cards in the deck
public void shuffle()
{
currentCard = 0;
for(int first = 0; first < deck.length; first++)
{
int second = randomNumbers.nextInt(NUMBER_OF_CARDS);
Card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
}
//Deal one card
public Card dealCard()
{
if(currentCard < deck.length)
{
return deck[currentCard++];
cardsInDeck--;
}
else
return null;
}
//Report the number of cards in the deck at any given time
public int numberOfCards()
{
System.out.println(cardsInDeck);
}
}
//CARD
public class Card
{
//Faces
public enum Face {Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King};
//Suits
public enum Suit {Hearts, Diamonds, Clubs, Spades};
private Face face;
private Suit suit;
//Constructor
public Card(Face cardFace, Suit cardSuit){
face = cardFace;
suit = cardSuit;
}
public String toString(){
return face + " of " + suit;
}
}
//DRIVER
public class DeckOfCardsTest
{
public static void main(String []args)
{
//DeckOfCards object
DeckOfCards myDeckOfCards = new DeckOfCards();
myDeckOfCards.shuffle();
//Deal and display a card
for(int i = 1; i <= 52; i++)
{
System.out.printf("%-20s",myDeckOfCards.dealCard());
if( i % 4 == 0) //New line every 4 cards
{
System.out.println();
}
}
}
}
Explanation / Answer
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.