Closed Thread Icon

Topic awaiting preservation: What is going on with this (Java) (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12198" title="Pages that link to Topic awaiting preservation: What is going on with this (Java) (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: What is going on with this (Java) <span class="small">(Page 1 of 1)</span>\

 
WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 05-01-2002 09:20

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;
}
}



InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 05-01-2002 12:17

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 05-01-2002 15:21

Nice call! I never knew static to work like that. Thanks for the information!

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 05-01-2002 15:31

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 05-01-2002 17:32

I simply removed the "static" from the two variables and it fixed the problem.

Your fix exactly.

synax
Maniac (V) Inmate

From: Cell 666
Insane since: Mar 2002

posted posted 05-01-2002 18:09

I notice that you call .toString() alot when you print off results from your methods. Now I could very well be wrong (Ini?) but I don't think it's necessary for you to append .toString() in order for you to print what you want. As long as you define your toString() method that should be enough.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 05-01-2002 19:08

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

synax
Maniac (V) Inmate

From: Cell 666
Insane since: Mar 2002

posted posted 05-02-2002 05:12

Hmmm, didn't know that. Yet another flaw in my education system.

« BackwardsOnwards »

Show Forum Drop Down Menu