-1

I have to make a class of a card, and another class of the deck. My deck class needs my card class. In my deck class, I have to build the deck in a separate method from main, which I did. Then I also have to print the deck in a separate method from main. Here is my problem: I tried using the typical toString method, which works fine in main. But I'm having trouble turning printing in it's own method. Can anyone tell me what I'm doing wrong in my print method? Any help is appreciated!

Card Class:

public class card
{
    private int face;
    private String suit;

    public card()
    {
        face = 0;
        String suit = " ";
    }

    public card(int face, String suit)
    {
        setFace(face);
        setSuit(suit);

    }

    public void setFace(int f)
    {
        if(f >= 1 && f <= 10)
            face = f;

        else
            face = 1;
    }

    public int getFace()
    {
        return face;
    }

    public void setSuit(String s)
    {
        if( s == "Hearts" && s == "Clubs" && s == "Diamonds" && s ==  "Ace")
            suit = s;
        else
            suit = "Hearts";
    }

    public String getColor()
    {
        return suit;
    }

}

Deck Class:

import java.util.*;

public class DeckOfCards
{
    public static void main(String[] arg)
    {
        card myCard = new card();

        card deck[] = new card[52];

        //System.out.println(Arrays.toString(deck));
        buildDeck(deck);

        System.out.print(myCard);

    }

    // method builds deck
    public static void buildDeck(card[] cardList)
    {
        cardList[0] = new card(1, "Hearts");
        cardList[1] = new card(2, "Hearts");
        cardList[2] = new card(3, "Hearts");
        cardList[3] = new card(4, "Hearts");
        cardList[4] = new card(5, "Hearts");
        cardList[5] = new card(6, "Hearts");
        cardList[6] = new card(7, "Hearts");
        cardList[7] = new card(8, "Hearts");
        cardList[8] = new card(9, "Hearts");
        cardList[9] = new card(10, "Hearts");
        cardList[10] = new card(11, "Hearts");
        cardList[11] = new card(12, "Hearts");
        cardList[12] = new card(13, "Hearts");

        cardList[13] = new card(1, "Clubs");
        cardList[14] = new card(2, "Clubs");
        cardList[15] = new card(3, "Clubs");
        cardList[16] = new card(4, "Clubs");
        cardList[17] = new card(5, "Clubs");
        cardList[18] = new card(6, "Clubs");
        cardList[19] = new card(7, "Clubs");
        cardList[20] = new card(8, "Clubs");
        cardList[21] = new card(9, "Clubs");
        cardList[22] = new card(10, "Clubs");
        cardList[23] = new card(11, "Clubs");
        cardList[24] = new card(12, "Clubs");
        cardList[25] = new card(13, "Clubs");

        cardList[26] = new card(1, "Diamonds");
        cardList[27] = new card(2, "Diamonds");
        cardList[28] = new card(3, "Diamonds");
        cardList[29] = new card(4, "Diamonds");
        cardList[30] = new card(5, "Diamonds");
        cardList[31] = new card(6, "Diamonds");
        cardList[32] = new card(7, "Diamonds");
        cardList[33] = new card(8, "Diamonds");
        cardList[34] = new card(9, "Diamonds");
        cardList[35] = new card(10, "Diamonds");
        cardList[36] = new card(11, "Diamonds");
        cardList[37] = new card(12, "Diamonds");
        cardList[38] = new card(13, "Diamonds");

        cardList[39] = new card(1, "Diamonds");
        cardList[40] = new card(2, "Diamonds");
        cardList[41] = new card(3, "Diamonds");
        cardList[42] = new card(4, "Diamonds");
        cardList[43] = new card(5, "Diamonds");
        cardList[44] = new card(6, "Diamonds");
        cardList[45] = new card(7, "Diamonds");
        cardList[46] = new card(8, "Diamonds");
        cardList[47] = new card(9, "Diamonds");
        cardList[48] = new card(10, "Diamonds");
        cardList[49] = new card(11, "Diamonds");
        cardList[50] = new card(12, "Diamonds");
        cardList[51] = new card(13, "Diamonds");

    }

    // method to print deck
    public String toString()
    {
        System.out.println(Arrays.toString(deck));
    }

} 
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28

3 Answers3

1

Your toString method should return a String, not print it.

So, replace this:

public String toString(){
     System.out.println(Arrays.toString(deck));
}

by something like this:

public String toString(){
     return Arrays.toString(deck);
}
Andres
  • 10,561
  • 4
  • 45
  • 63
  • Hey thanks, it says that it can't find the symbol. So for some reason it's not calling my deck in my main, or my deckbuild method and I'm not sure why. – user3034450 Apr 28 '14 at 20:28
1

Your deck[] is defined as a local variable in your main method. You need to move it out and make it a static class-level variable.

Also, you probably shouldn't call your printing method toString(). toString is an existing method on Object that you are shadowing here.

The Real Bill
  • 14,884
  • 8
  • 37
  • 39
azurefrog
  • 10,785
  • 7
  • 42
  • 56
0

I personally would do something like this:

public class Cards {

    public static void main(String[] args) {

        DeckOfCards deck = new DeckOfCards();
        System.out.println( deck );
    }
}


public class CardSuit {

    public static final CardSuit CLUBS = new CardSuit("Clubs");
    public static final CardSuit DIAMONDS = new CardSuit("Diamonds");
    public static final CardSuit HEARTS = new CardSuit("Hearts");
    public static final CardSuit SPADES = new CardSuit("Spades");

    private String suit;

    private CardSuit( String suit ){
        this.suit = suit;
    }

    @Override public String toString(){ return this.suit; }
}

public class CardFace {

    private String face;

    public CardFace( int value ){

        this.face = this.convertValueToFace( value );
    }

    private String convertValueToFace( int value ){

        if ( value > 1 && value < 11 ){
            return Integer.toString( value );
        }

        switch( value ){
            case 11 : { return "J"; }
            case 12 : { return "Q"; }
            case 13 : { return "K"; }
            case 1 : { return "A"; }
        }

        throw new IllegalArgumentException( 
            "Card face value must be between 1 and 13"
        );
    }

    @Override public String toString(){ return this.face; }
}

public class Card {

    private CardSuit suit;
    private CardFace face;

    public Card( CardSuit suit, CardFace face ){
        this.suit = suit;
        this.face = face;
    }

    @Override public String toString(){ 
        return this.face + " of " + this.suit; 
    }
}

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DeckOfCards {

private List<Card> cards;

    public DeckOfCards(){
        this.createDeck();
    }

    private void createDeck(){

        this.cards = new ArrayList<Card>();

        List<CardSuit> suits = new ArrayList<CardSuit>();
        suits.add( CardSuit.HEARTS );
        suits.add( CardSuit.DIAMONDS );
        suits.add( CardSuit.CLUBS );
        suits.add( CardSuit.SPADES );

        List<CardFace> faces = new ArrayList<CardFace>();

        int valueStart = 1;
        int valueEnd = 13;

        for ( int value = valueStart; value <= valueEnd; value++ ){
            faces.add( new CardFace( value ) );
        }

        Iterator<CardSuit> suitIterator = suits.iterator();
        while( suitIterator.hasNext() ){

            CardSuit suit = suitIterator.next();

            Iterator<CardFace> faceIterator = faces.iterator();
            while( faceIterator.hasNext() ){
                Card card = new Card( suit, faceIterator.next() );
                this.cards.add( card );
            }
        }
    }

    @Override public String toString(){
        String result = "Card Deck: [\n";
        Iterator<Card> cardIterator = this.cards.iterator();
        while( cardIterator.hasNext() ){
            result += "\t" + cardIterator.next().toString() + "\n";
        }
        result += "]";
        return result;
    }

}
scibuff
  • 13,377
  • 2
  • 27
  • 30