Kiran Monk
Kiran Monk

Reputation: 13

Constructor for card class wont compile

im making a card game for my assignment but am having issues with making my card class compile, i get the following message, package myDeck does not exist and im really not sure what im doing wrong, here is my code. please can anyone help?

DeckofCards class

import java.util.Random;


public class DeckOfCards
{

public int suit;
public int card;
public Random r;
public final int MAXSUIT = 3;
public final int MAXCARD = 12;


/**
 * Constructor for objects of class DeckOfCards
 */
public DeckOfCards()
{
    // initialise instance variables
    r = new Random();
    shuffleDeck();
}

public void shuffleDeck()
{
    suit = r.nextInt(MAXSUIT);
    card = r.nextInt(MAXCARD);
}

public int getCard()
{
    return card;
}

public int getSuit()
{
    return suit;
}

}

Card Class

import java.util.Random;

public class Card
{

private DeckOfCards myDeck;
public int card;
public int suit; 
public String cardName;
public String suitName;


public Card()
{
   card = new myDeck.getCard();
   suit = new myDeck.getSuit();      

}

public String showCard()
{
    return (cardName+ " of "+suit);
}

public void changeCard()
{
   new Card();
}

public String getCardName()
{
    return cardName;
}

public String getSuitName()
{
    return suitName;
}

public void setCardName()
{
    switch(card)
    {
        case 0  :
        cardName = "2";
                    break;
        case 1  :
        cardName = "3";
                    break;
        case 2  :
        cardName = "4";
                    break;
        case 3  :
        cardName = "5"; 
                    break;
        case 4  :
        cardName = "6";
                    break;
        case 5  :
        cardName = "7";
                    break;
        case 6  :
        cardName = "8";
                    break;
        case 7  :
        cardName = "9";
                    break;
        case 8  :
        cardName = "10";
                    break;
        case 9:
        cardName = "Jack";
                    break;
        case 10 :
        cardName = "Queen";
                    break;
        case 11 :  
        cardName = "King";
                    break;
        case 12 : 
        cardName = "Ace";
                    break;
        default :
    }
}

public void setSuitName()
{
       switch(suit)
       {
           case 0 :
           suitName = "Diamonds";
                        break;
           case 1 :
           suitName = "Hearts";
                        break;
           case 2 :
           suitName = "Spades";
                        break;
           case 3 :
           suitName = "Clubs";
                        break;
           default :
           suitName =  "No suit";
                        break;
        }
}
}

Upvotes: 0

Views: 104

Answers (2)

laune
laune

Reputation: 31300

Adding something to the hints that the design can stand improvement. If someone thinks this shouldn't be here: comment, and I'll promptly remove it. - What one usually does is to create a Deck as a Collectíon of 52 Card objects:

class Card {
   Suit suit;
   Rank rank;
   Card( Suit suit, Rank rank ){...}
}
class Deck {
   List<Card> cards = ...
   Deck(){
       for( Suit suit: EnumSet.allOf(Suit.class) ){
           for( Rank rank: EnumSet.allOf(Rank.class) ){
               cards.add( new Card( suit, rank ) );
           }
       }
   }
}

You can shuffle the deck before you deal a hand.

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62874

You're using myDeck before it's initialized. It has to rather be:

public Card() {
   myDeck = new DeckOfCards();
   card = myDeck.getCard();
   suit = myDeck.getSuit();
}

As a side note, there's no need to maintain two additional members (card and suit), since you can get this data from the myDeck variable.

You should also reconsider your design, because a the DeckOfCard should be dependent on the Card class, not otherwise. Currently, whenever you create a Card object, another DeckOfCards is created, too. Which is semantically wrong.

If I were you, I would put both classes in one package and decorate the Card constructor with the protected modifier, so that it can be instantiated only within this package. Also, I would remove the myDeck member from the Card class and introduce a Set<Card> member in the DeckOfCards class.

Upvotes: 3

Related Questions