I was running the test program and I experienced something that surprised me and I wasn't sure what was going on with the line.
Deck.java lines 29-32 are suposed to propogate the card array with all the values in a standard deck of card by producing a new instance of card and placing the appropriate value in it.
In my program I test Deck and what I get printed is a string of just "King of Spades" which is the last data entered.
I am supposing that for some reason a new instance is not being created, it is just overwriting all the memory locations with the newest version. I have checked individual card data and as it runs and it seems this is happening. I can place System.out.println(card[0].toString()) at line 31 and as the new card is created the new data is stored for the value of card[0]. I don't know why this is happening. It is really baffelling me.
Anyone have any ideas.
Card.java
code:
public class Card
{
private static int value;
private static char suit;
public static void main(String[] args)
{
System.out.println("Lets Create a Sample Card: Ace of Hearts");
System.out.println();
Card newCard = new Card(1,'H');
System.out.println("Lets see what we have : " + newCard.toString());
System.out.println("Lets get the value : " + newCard.getValue());
System.out.println("Lets get the value string: " + newCard.getValueString());
System.out.println("Lets get the suit : " + newCard.getSuit());
System.out.println("Lets get the suit string : " + newCard.getSuitString());
}
public Card()
{
value = 1;
suit = 'H';
}
public Card(int thisValue, char thisSuit)
{
value = thisValue;
suit = thisSuit;
}
public char getSuit()
{
return suit;
}
public String getSuitString()
{
switch (suit)
{
case 'H':
return "Hearts";
case 'C':
return "Clubs";
case 'D':
return "Diamonds";
case 'S':
return "Spades";
}
return "";
}
public int getValue()
{
return value;
}
public String getValueString()
{
switch (value)
{
case 1:
return "Ace";
case 11:
return "Jack";
case 12:
return "Queen";
case 13:
return "King";
default:
return "" + value + "";
}
}
public void setCard(int thisValue, char thisSuit)
{
value = thisValue;
suit = thisSuit;
}
public void setSuit(char thisSuit)
{
suit = thisSuit;
}
public void setValue(int thisValue)
{
value = thisValue;
}
public String toString()
{
return getValueString() + " of " + getSuitString();
}
}
Deck.java
code:
public class Deck
{
Card[] card;
int currentCard;
public static void main(String[] args)
{
System.out.println("Lets create a sample deck:");
Deck newDeck = new Deck();
System.out.println(newDeck.toString());
/*System.out.println("Lets deal a few cards:");
for (int i = 0;i < 5;i++)
System.out.println("Card " + i + " : " + newDeck.dealCard().toString());
System.out.println("Now lets shuffel the deck:");
newDeck.shuffel();
System.out.println(newDeck.toString());
System.out.println("Lets deal a few cards:");
for (int i = 0;i < 5;i++)
System.out.println("Card " + i + " : " + newDeck.dealCard().toString());*/
}
public Deck()
{
card = new Card[52];
currentCard = 0;
for (int i = 0;i < 13;i++)
{
card[i] = new Card(i+1,'H');
card[i+13] = new Card(i+1,'C');
card[i+26] = new Card(i+1,'D');
card[i+39] = new Card(i+1,'S');
}
}
public Card dealCard()
{
Card temp = card[currentCard];
currentCard++;
return temp;
}
public Card getCard(int i)
{
return card[i];
}
public void shuffel()
{
currentCard = 0;
int times = (int)(Math.random() * 400);
for (int i = 0;i < times;i++)
{
int card1 = (int)(Math.random() * 52);
int card2 = (int)(Math.random() * 52);
Card temp = card[card1];
card[card1] = card[card2];
card[card2] = temp;
}
}
public String toString()
{
String temp = "";
for (int i = 0;i < card.length;i++)
temp = temp + "" + card[i].toString() + "\n";
return temp;
}
}