Results 1 to 3 of 3
  1. #1
    VelvetMirror is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Deck of cards problem

    I'm following the Official Java Tutorial and I'm doing an exercise in order to incorporate enums to a Deck of Cards example.

    I have 5 classes: Rank (an enum of the ranks), Suit (an enum of the suits), Card (a class describing a single Card), Deck (a class describing a deck of cards), and DisplayDeck (which displays the cards in the decks).

    The solution solves the Card class, the Deck class and the enum classes (Rank and Suit) so I have written a DisplayDeck class in order to show the deck of cards.

    Here are my classes, in bold it's the new code I've added (at the last two classes) and although it works fine, I would like to know if it is the right way to do it or if there is a better or simpler way to go around it:

    1. Rank class:


      Java Code:
      package deckcardswithenum;
      
      public enum Rank {
      
          ACE, DEUCE, THREE, FOUR, FIVE, SIX, SEVEN,
          EIGHT, NINE, TEN, JACK, QUEEN, KING
      }
    2. Suit class:


      Java Code:
      package deckcardswithenum;
      
      public enum Suit {
      
          SPADES, HEARTS, CLUBS, DIAMONDS
      }
    3. Card class:


      Java Code:
      package deckcardswithenum;
      
      public class Card {
      
          private final Suit suit;
          private final Rank rank;
      
          public Card(Rank rank, Suit suit) {
              this.rank = rank;
              this.suit = suit;
          }
      
          public Rank getRank() {
              return rank;
          }
      
          public Suit getSuit() {
              return suit;
          }
      
          @Override
          public String toString() {
              return rank + " of " + suit;
          }
      }
    4. Deck class:


      Java Code:
      package deckcardswithenum;
      
      public class Deck {
      
          private static Card[] cards = new Card[52];
      
          public Deck() {
              int i = 0;
      
              for (Suit suit : Suit.values()) {
                  for (Rank rank : Rank.values()) {
                      cards[i++] = new Card(rank, suit);
                  }
              }
          }
      [b]
          public Card[] getCards() {
              return cards;
          }[/b]
      }
    5. DisplayDeck class:


      Java Code:
      [b]package deckcardswithenum;
      
      public class DisplayDeck {
      
          public static void main(String[] args) {
              Deck deck = new Deck();
              Card[] cards = deck.getCards();
      
              int i = 0;
      
              for (Suit suit : Suit.values()) {
                  for (Rank rank : Rank.values()) {
                      System.out.println(cards[i++]);
                  }
              }
          }
      }[/b]

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Why don't you simply make that loop in your last class like this?

    Java Code:
    for (Card c: deck.getCards())
       System.out.println(c);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    VelvetMirror is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    It seemed to me that my code was redundant but I couldn't point where, I didn't understand well enough the use of "for" loop for collections.

    You are right, that's a far better solution.

    Thanks.

Similar Threads

  1. Creating a deck of cards and printing in console
    By Danieldcc in forum New To Java
    Replies: 7
    Last Post: 10-12-2010, 07:37 AM
  2. Poker Simulation (shuffling a deck of cards?)
    By StateofMind in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 08:59 PM
  3. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 05:47 PM
  4. Problem with making/sorting a deck of cards
    By Franneldort in forum New To Java
    Replies: 9
    Last Post: 11-07-2008, 12:47 AM
  5. creating a deck of cards using a linked list
    By boomba88 in forum New To Java
    Replies: 2
    Last Post: 09-11-2008, 03:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •