0
votes

So i have a class called PlayingCard which creates an object which contains int rank and int suit (to simulate a playing card).

public class PlayingCard 
{
private int rank;
private int suit;


public PlayingCard(int rank1, int suit1) //PlayingCard Constructor
{
    rank = rank1;
    suit = suit1;
}


public int getRank() //Get method to retrieve value of card Rank
{
   if(rank >0 && rank <15)
   {
       return rank;
   }
   else
       return 0;
}


public String getSuit() //Get method to retrieve value of card Suite
{

    while (suit >0 && suit <5)
    {
        if (suit == 1)
        {
            return "Clubs";
        }
        else if (suit == 2)
        {
            return "Diamonds";
        }
        else if (suit == 3)
        {
            return "Hearts";
        }
        else
        {
            return "Spades";
        }
    }

    return "Invalid suit";
}


@Override //Overrides default Java toString method
public String toString()
{
    String strSuit = "";

    switch(suit) 
    {
        case 1: strSuit = "Clubs";
            break;
        case 2: strSuit = "Diamonds";
            break;
        case 3: strSuit = "Hearts";
            break;
        case 4: strSuit = "Spades";
            break;
        default: strSuit = "Invalid Suit";
            break;
    }

    String output = getClass().getName() + "[suit = " +strSuit + ", rank = "
                    +rank + "]";

    return output;
}


public String format() //Allows individual cards to be displayed in a
{                     //Specific format.
    String strSuit = "";

    switch(suit) 
    {
        case 1: strSuit = "Clubs";
            break;
        case 2: strSuit = "Diamonds";
            break;
        case 3: strSuit = "Hearts";
            break;
        case 4: strSuit = "Spades";
            break;
        default: strSuit = "Invalid Suit";
            break;
    }

    return rank + " of " + strSuit + ", ";
}

@Override
public boolean equals(Object y) //Method for evaluating Object equality.
{

    if (getClass() != y.getClass()) //Checks if both object are from the
    {                              //Same class.
        return false;
    }

    if (y == null) //Checks if second object is empty or non-existent.
    {
        return false;
    }

    PlayingCard other = (PlayingCard) y;
    return rank == other.rank && suit == other.suit;
}
}

I then need to create a program called PackBuilder which should simulate building a deck of cards using my class.

The problem is im not sure how to give a new name to each object. I thought of something like this with arrays:

while(rank < 15)
        {
            PlayingCard cardDeck[1] = new PlayingCard(rank, suit);
        }

But it says that cardDeck is already defined (im not sure if i may have just done it wrong, or if using an array wont work)

that name scheme i wanted would be like 'card1' 'card2' 'card3' and so forth until i have 52 cards each with their own suit/rank combo to create a deck of cards.

3
Array indexes do not and can not have a name. Array indexes are accessed with a number.Radiodef

3 Answers

1
votes

You are attempting to assign a PlayingCard to the second element (index 1) in the cardDeck array, every time. In addition, the cardDeck array is being immediately discarded at the bottom of each iteration of your while loop. It's only scoped to exist inside of the loop.

Change this

while(rank < 15)
    {
        PlayingCard cardDeck[1] = new PlayingCard(rank, suit);
    }

to something like this

PlayingCard[] cardDeck = new PlayingCard[52];

int i = 0;
while(rank < 15)
    {
        cardDeck[i++] = new PlayingCard(rank, suit);
    }

To avoid an infinite loop, change

cardDeck[i++] = new PlayingCard(rank, suit);

to

cardDeck[i++] = new PlayingCard(rank++, suit);

There may be other problems, but this is a good one to start with.

0
votes

Do you have to use a class? Would be better to do this as a set of 3 enums. The get method you have checking the number and returning a string for the suit is all kinds of wrong. If suite were an enum, you could refer to it by name and number (ordinal).

Two nested loops is the simplest solution to the building part.

However, if you want a deck of named cards, so you could for instance say 'if card==QueenOfSpades, you are going to have to do something more creative, e.g. nested loops where you take the names from the rank and suit and concatenate them with Of in between. This is what makes this question interesting: clearly you cannot do this. You cannot programmatically name an enum.

Which means you could make your Deck enum have 52 named elements, e.g. DeuceOfSpades, DeuceOfHearts on up through AceOfSpades, etc.

Another option would be to do enums for rank and suit and then have the class deck generate the cards, and hold them inside, but then have a way to get the cards by name, e.g. deck.getAceOfHearts(). Then it could also just serve up cards randomly through some kind of iterator interface, e.g. getNextCard().

0
votes

deck of cards (for convenience):
assuming 14 cards per suit (I don't know exact number of ranks, I assume 14)

given rank and suit of card, you may get it through cardDeck[(rank-1)*4 + (suit-1)]

PlayingCard cardDeck = new PlayingCard[56];

int rank, suit, i = 0;

for(rank=1; rank<=14; rank++)
{

  for(suit=1; suit<=4; suit++)

  cardDeck[i++] = new PlayingCard(rank, suit);    


}