Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't seem to print an arraylist full of objects/enums
    text
    copied!<p>I was trying to create a card game golf for fun and to practice my java skills. I'm trying to use enums for my card values and suites. Those values are held in a constructor, named Card, in the Card Class. </p> <p>The problem I'm running into to is printing my arraylist Deck that holds all my individual cards. This method can be found in the DeckOfCards Class . I want to see if my program is creating a full deck of cards. Thanks in advance for the help!</p> <p>DeckOfCards Class</p> <pre><code>package finalProjectGolf; // Deck class represent a deck of playing cards. import java.util.ArrayList; import java.util.Collections; public class DeckOfCards { ArrayList&lt;Card&gt; deck = new ArrayList&lt;Card&gt;(); //array of Card objects private int currentCard; // index of next Card to be dealt (0-51) private static int numDecks = 1; public int getCurrentCard() { return currentCard; } public void setCurrentCard(int currentCard) { this.currentCard = currentCard; } private static final int NUMBER_OF_CARDS = 52 * numDecks; //constant # of Cards //constructor fill deck of Cards public DeckOfCards(){ currentCard = 0; //set currentCard so first Card dealt is deck[0] //Card Index int c = 0; //for each deck for (int d = 0; d &lt; numDecks; d++){ //for each suit for (int s = 0; s &lt; 4; s++){ // for each number for (int n = 1; n &lt;= 13; n++){ //add a new card to the deck deck.add(new Card(CardValue.values()[n],Suit.values()[s])); //when using Enums java makes arrays automatically and you can use them by .values() c++; }}}//end for loop }//end DeckOfCards constructor //shuffle deck of Cards with one-pass algorithm public void shuffle() { Collections.shuffle(deck); } public int points(){ int value = deck.get(currentCard).getCardValue().getCardValue(); return value; } //deal one Card public Card dealCard(int currentCard) { //determine whether Cards remain to be dealt if( currentCard &lt; deck.size() ){ return deck.get(currentCard); //return current Card in array } else return null; // return null to indicate that all Cards were dealt }//end method dealCard public void printDeck(){ { currentCard = 0; //set currentCard so first Card dealt is deck[0] //Card Index int c = 0; //for each deck for (int d = 0; d &lt; numDecks; d++){ //for each suit for (int s = 0; s &lt; 4; s++){ // for each number for (int n = 1; n &lt;= 13; n++){ //add a new card to the deck System.out.printf(""); //when using Enums java makes arrays automatically and you can use them by .values() c++; }}}//end for loop }//end DeckOfCards constructor } }// end class DeckOfCards </code></pre> <p>Card Class</p> <pre><code>package finalProjectGolf; public class Card { private Suit suit; private CardValue cardValue; public Card (CardValue cardValue, Suit suit) //constructor of Card, holds Card value and it's suit { this.cardValue = cardValue; this.suit = suit; } public Suit getSuit() { return suit; } public void setSuit(Suit suit) { this.suit = suit; } public CardValue getCardValue() { return cardValue; } public void setCardValue(CardValue cardValue) { this.cardValue = cardValue; } public String toString(){ return cardValue + " of " + suit; }// end method toString } </code></pre> <p>CardValue Class</p> <pre><code>package finalProjectGolf; public enum CardValue { ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13); private int cardValue; private CardValue (int value) { this.cardValue = value; } public int getCardValue() { return cardValue; } } </code></pre> <p>Suit Class</p> <pre><code>package finalProjectGolf; public enum Suit { HEARTS(1), SPADES(2), CLUBS(3), DIAMONDS(4); private int suit; private Suit (int value) { this.suit = value; } public int getCardSuit() { return suit; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload