codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  this is weird  jaymo33 at 01:32 on Wednesday, June 21, 2006
 

I am a beginner at java programming and am having trouble with this poker program. In the main method I am testing Other.copyArray(Card[], Card[]). Instead of doing what it is supposed to do, (make the second Card array in the parameter a clone of the first Card array in the parameter) the second Card array prints as:
3 s
3 s
The cards second Card array in the parameter always become clones of the last card in the first Card array in the parameter. I checked my logic in copyArray() and have successfully tested all other methods, but it still does not work. Any help or tips would be great. Please, I am a begginer. Be patient with my lack of knowledge.


public class Main
{
static Dealer theDealer = new Dealer();
static Other others = new Other();
public static void printArray(Card[] array)
{
for(int i = 0; i <= array.length - 1; i++)
{
pl(array.getValueInt() + " " + array.getSuitLetter());
}
}
public static void main(String[] args)
{
Card card1 = new Card(2, "c");
Card card2 = new Card(3, "s");
Card card3 = new Card(4, "s");
Card[] testArray1 = {card1, card2};
Card[] testArray2 = {card3, card3};
others.copyArray(testArray1, testArray2);
printArray(testArray2);
}
class Card
{
String suit = "";
int value = 0;

Card(int v, String s)
{
value = v;
suit = s;
}
int getValueInt()
{
if(this.value > 1 || this.value < 15)
return this.value;
return -1;
}
void setValue(int newValue)
{
this.value = newValue;
}
void setSuit(String newSuit)
{
this.suit = newSuit;
}
String getSuitLetter()
{
if(this.suit.equals("s") || this.suit.equals("d") || this.suit.equals("h") || this.suit.equals("c"))
return this.suit;
return "error";
}
void clone(Card newCard)
{
this.setValue(newCard.getValueInt());
this.setSuit(newCard.getSuitLetter());
}
}
class Other
{
void copyArray(Card[] array1, Card[] array2)
{
if(array1.length == array2.length)
{
for(int i = 0; i <= array1.length - 1; i++)
array2.clone(array1);
}
}
}

  Re: this is weird  crwood at 20:32 on Thursday, June 29, 2006
 


public class CopyTest
{
static Other others = new Other();

public static void printArray(Card[] array)
{
for(int j = 0; j < array.length; j++)
{
System.out.print(array[j].getValueInt() + " " +
array[j].getSuitLetter());
if(j < array.length-1)
System.out.print(", ");
}
System.out.println();
}

public static void main(String[] args)
{
Card card1 = new Card(2, "c");
Card card2 = new Card(3, "s");
Card card3 = new Card(4, "s");
Card[] testArray1 = { card1, card2 };
Card[] testArray2 = { card3, card3 };
// before
System.out.print("testArray1 = ");
printArray(testArray1);
System.out.print("testArray2 = ");
printArray(testArray2);
// make the change
// others.copyArray(testArray1, testArray2);
others.copyWithClone(testArray1, testArray2);
// after
System.out.print("testArray1 = ");
printArray(testArray1);
System.out.print("testArray2 = ");
printArray(testArray2);
}
}

class Card
{
String suit = "";
int value = 0;

Card(int v, String s)
{
value = v;
suit = s;
}

int getValueInt()
{
if(this.value > 1 || this.value < 15)
return this.value;
return -1;
}

void setValue(int newValue) { this.value = newValue; }

void setSuit(String newSuit) { this.suit = newSuit; }

String getSuitLetter()
{
if(this.suit.equals("s") || this.suit.equals("d") ||
this.suit.equals("h") || this.suit.equals("c"))
return this.suit;
return "error";
}

/**
* This method assigns new values to the instance members of this class
* so when this Card is accessed in other arrays it will not be the same.
* This is okay but doesn't do what you want it to do.
* (It alters the Card values of your arrays.)
*/
void clone(Card newCard)
{
this.setValue(newCard.getValueInt());
this.setSuit(newCard.getSuitLetter());
}

/**
* Make a Card object like this Card and return it to the caller.
* This Card remain unchanged so the arrays that contain it will
* not be changed as an unwanted side-effect.
*/
protected Object clone()
{
return new Card(this.value, this.suit);
}
}

class Other
{
void copyArray(Card[] array1, Card[] array2)
{
if(array1.length == array2.length)
{
for(int j = 0; j < array1.length; j++)
array2[j].clone(array1[j]);
}
}

void copyWithClone(Card[] array1, Card[] array2)
{
if(array1.length == array2.length)
{
for(int j = 0; j < array1.length; j++)
array2[j] = (Card)array1[j].clone();
}
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: Function to check the pattern(i.e 1A-2B-3C)
•  Re: problem with Exception
•  Re: Turning java class into application
•  Re: this is weird
•  Server Name or Address could not be resolved?
•  Re: How can I read ASCII data file in C++
•  Sending automated emails
•  Re: How to kill framesets
•  What is This?


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006