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.