Hi All,
I was following the Really Big Index Tutorial and was looking at the section on 'Enums'. I have a question about using a set of classes defined in that section for a deck of cards. The following four classes are defined (Deck.class, Card.class, Suit.class, Rank.class):
Deck.class
Card.class
Suit.class
Rank.class
I want to create a main program (DisplayDeck.java) that creates a deck and lists the cards in the deck. How do I incorporate Card.toString to achieve this? Thanks.
DisplayDeck.class
Cheers,
ScKaSx
I was following the Really Big Index Tutorial and was looking at the section on 'Enums'. I have a question about using a set of classes defined in that section for a deck of cards. The following four classes are defined (Deck.class, Card.class, Suit.class, Rank.class):
Deck.class
import java.util.*;
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);
}
}
}
}
Card.class
public class Card {
private final Rank rank;
private final Suit suit;
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Suit getSuit() {
return suit;
}
public Rank getRank() {
return rank;
}
public String toString() {
return rank + " of " + suit;
}
}
Suit.class
public enum Suit {
DIAMONDS,
CLUBS,
HEARTS,
SPADES
}
Rank.class
public enum Rank {
DEUCE, THREE, FOUR, FIVE, SIX, SEVEN,
EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
}
I want to create a main program (DisplayDeck.java) that creates a deck and lists the cards in the deck. How do I incorporate Card.toString to achieve this? Thanks.
DisplayDeck.class
import java.util.*;
public class DisplayDeck {
public static void main (String[] args) {
private static Deck deck = new Deck();
System.out.println(Card.toString) //?????
}
}
Cheers,
ScKaSx