Results 1 to 3 of 3
Thread: Deck of cards problem
- 02-16-2011, 06:46 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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:
- Rank class:
Java Code:package deckcardswithenum; public enum Rank { ACE, DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING } - Suit class:
Java Code:package deckcardswithenum; public enum Suit { SPADES, HEARTS, CLUBS, DIAMONDS } - 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; } } - 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] } - 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]
- 02-16-2011, 06:56 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Why don't you simply make that loop in your last class like this?
kind regards,Java Code:for (Card c: deck.getCards()) System.out.println(c);
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-16-2011, 08:02 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Creating a deck of cards and printing in console
By Danieldcc in forum New To JavaReplies: 7Last Post: 10-12-2010, 07:37 AM -
Poker Simulation (shuffling a deck of cards?)
By StateofMind in forum New To JavaReplies: 4Last Post: 04-06-2010, 08:59 PM -
Deck of Cards
By khunmato in forum New To JavaReplies: 13Last Post: 09-06-2009, 05:47 PM -
Problem with making/sorting a deck of cards
By Franneldort in forum New To JavaReplies: 9Last Post: 11-07-2008, 12:47 AM -
creating a deck of cards using a linked list
By boomba88 in forum New To JavaReplies: 2Last Post: 09-11-2008, 03:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks