I have the ArrayList deck in Class Deck.I want to fill the ArrayList with Card objects.
Also I want to fill the attributes of the Card objects.
I added a for loop to see the results,and the ArrayList had 5 objects with NULL in name and 0 in value.
(FillDeck has been called in main function)
import java.util.ArrayList;
public class Deck {
private ArrayList<Card> deck=new ArrayList();
public Deck(){
}
public Deck(ArrayList<Card> deck ){
this.deck = deck;
}
//hearts,spades,diamonds,clubs
Card card1 = new Card("A hearts",11,true);
Card card2 = new Card("2 hearts",2,true);
Card card3 = new Card("3 hearts",3,true);
Card card4 = new Card("4 hearts",4,true);
Card card5 = new Card("5 hearts",5,true);
public void filldeck(){
deck.add(card1);
deck.add(card2);
deck.add(card3);
deck.add(card4);
deck.add(card5);
for (int i=0; i<deck.size(); i++){
System.out.println(deck.get(i).getName());
System.out.println(deck.get(i).getValue());
}
}
}
public class Card {
private String name;
private int value;
private boolean samecard = true ;
public Card(){
}
public Card(String name,int value,boolean samecard){
name = this.name;
value = this.value;
samecard = this.samecard;
}
public String getName(){
return name;
}
public int getValue(){
return value;
}
public boolean getSamecard(){
return samecard;
}
}
Because you are seeing the default values of the fields. To fix that you have to actually assign the values e.g.
public Card(String name,int value,boolean samecard){
this.name = name;
this.value = value;
this.samecard = samecard;
}