4

For my class lab we are to be making a Simple card deck tool that produces a standard card deck and draws 5 of them. We just learned ArrayList and how to make our own classes, but now I'm trying to put them together and it's just not working out. Here's my basic card class:

public class Card
{
private String Number;
private String Suit;

public Card()
{
Number = "Joker";
Suit = "Card";
}

I also have an overloaded card class, but excluding to save space. Next is the basic deck class:

public class Deck
{
private ArrayList<Card> CardDeck = new ArrayList<Card>();

public Deck()
{
CardDeck.clear();
CardDeck.add(new Card("Ace","Hearts"));
}

finally, I have my client class. Right now, I'm just trying to print the generated card [Ace,Hearts].

In my card class, I have a method to print out the card's values:

public String print(Deck a) 
{
  return (this.Number+" of "+this.Suit);
}

However, I'm struggling with how to print an arraylist where the list is of classes. I know right now things are a little jumbled (I'm not going to print the deck) but I figured jumping this hurdle was more important than continuing and making a hand arraylist. The problem is the same, the variables are just named different.

I tried looking on the site for similar questions, but most are either just String objects or not using arraylist alltogether.

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
formn
  • 107
  • 2
  • 7
  • 1
    You can print the contents of a List by iterating over its elements with a [foreach loop](http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html). If you google "_java foreach loop_" you should find some helpful info. In your case it looks like you would iterate over each card in the list with a foreach a loop. Inside the loop you would invoke the _print()_ method of each card. Since this is obviously homework, I'll refrain from providing a copy/paste answer. – jahroy Apr 12 '13 at 06:30
  • I would also suggest you read about [toString()](http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java) method. Also read about [java naming conventions](http://www.javapractices.com/topic/TopicAction.do?Id=58). – predi Apr 12 '13 at 06:35
  • @jahroy I appreciate your integrity. I'm not using this site to cheat or find easy answers. I'm here to learn. Thank you for not just giving the answer. @predi As I just posted a little further down, I was under the impression that altering the `toString()` method would mess up unrelated sysout statements. I'm guessing that's not correct. We've yet to cover naming standards. I'll bookmark that for reading later since it will make reading the code way easier should I ever come back to this little program. – formn Apr 12 '13 at 06:50

6 Answers6

3

First, a little terminology: Your ArrayList doesn't contain classes, it contains references to instances of the Card object. The type Card is used in the ArrayList declaration to indicate that everything in that ArrayList is a Card, but nowhere else is Card's class type referenced.

Now, if you just want to print out a card to the console, I recommend overriding the toString method for convenience. Currently, if you try:

System.out.println(aCard);

you'll get a memory address. Override the toString of Card and you can customize this output. For example:

public String toString() { //In your Card class
    return "["+this.Number+", "+this.Suit+"]";
}

Try printing out just one Card so you can see how this works.

Then, to iterate over each member of CardDeck, simply use the for each syntax:

for(Card c : CardDeck)
    System.out.println(c);
  • This is probably why I couldn't find anything when searching. Terminology is everything. Thank you for your input! I was trying to avoid overriding the `toString` because for some reason in my head I believed it would mess up the normal sysout statements. so just to clarify, since Deck is just an arraylist of objects Card, I can use `Deck.CardDeck.get(0)` to specify a certain card? Or would I go about that differently? – formn Apr 12 '13 at 06:41
  • Well, referencing it via `Deck.CardDeck.get` would imply two things: first, that CardDeck is public in Deck, and second, that CardDeck is static in Deck. But, as a general gist of things, yes –  Apr 12 '13 at 06:48
  • `ArrayList` contains references to instances of objects, not the objects themselves. – predi Apr 12 '13 at 07:09
  • @predi Ehh, small details, small details... Edited –  Apr 12 '13 at 07:10
  • 1
    That's where the devil usually hides. – predi Apr 12 '13 at 07:20
3

If you want to print the value from Cred class then fine, But if you want to print this from outside the class then yo need to add a public getter method of any private variable.
Now if you just try to print the object value from outside then you need to override the toString() method:

public String toString(){
    return("Number = "+this.Number+" and Suit = "+this.Suit);
}

If you want to print outside the class,then
Change your Card class like this:

public class Card
{
private String Number;
private String Suit;

public Card(String Number,String Suit)
{
this.Number=Number;
this.Suit=Suit;
}

public String getNumber() {
    return Number;
}

public void setNumber(String number) {
    Number = number;
}

public String getSuit() {
    return Suit;
}

public void setSuit(String suit) {
    Suit = suit;
}
}

Then then you can print the value like this:

List<Card> cardList = new ArrayList<Card>();
//Put some value in the list.
cardList.add(new Card("Ace","Hearts"));
//And so on..
    for (Card card : cardList) {
        System.out.println("Number = "+card.getNumber()+" Suit = "+card.getSuit());
    }
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
  • Making a public method seems like the smart choice for printing outside its class since that's what I'll be doing. – formn Apr 12 '13 at 07:01
1

You can iterate the ArrayList and the print each Card the way you want.

for (Card card : cardDesk) {
    System.out.println(String.format("Card number: %s, suit: %s", card.getNumber(), card.getSuit());
}

Similarly you can use StringBuilder instead of Sysout if you want to return the print content and not output to the console.

Also please use CamelCase to name your variables as its best practice.

Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71
1

You should not have a print(Deck a) method in your Card class, a Card class must only know about how to print it self (called single responsibility principle in object oriented programming)

assuming that you have a print method (also think about overriding toString method)

public String print(){
    return (this.Number+" of "+this.Suit);
}

Be aware that it does not take any parameters, since a Card instance knows itself.

Also add a printCards method in your Deck class

public void printCards(){
    for (Card card: CardDeck){
         System.out.println(card.print());
    }
}
Serkan Arıkuşu
  • 5,549
  • 5
  • 33
  • 50
1

Just iterate your ArrayList CardDeck and call each elements print() method

for (Card card : CardDeck) {
     card.print();
}
Mohan Raj B
  • 1,015
  • 7
  • 14
1

First of all your Card constructor is bad, it should be:

public Card(String number, String suit) {
this.number = number;
this.suit = suit;
}

And accessor methiod for Card:

public String getNumber(){
    return number;
}

Same for Suit

Now you can create a card and add it to the deck:

CardDeck.add(new Card("Ace","Hearts"));

And now to print the Deck;

StringBuilder b = new StringBuilder();
for(Card c: cardDeck){
    b.append(c.getNumber).append(c.getSuit);
}
return b.toString();
b4gam
  • 61
  • 2