Lab 11: The Java Enum Type

In this lab, you will practice using the Java enum type by creating a deck of cards.

The enum type is used to create a type that has a limited range of values. For example, you can create a Suit type that can only contain 4 values, clubs, diamonds, hearts, and spades.

public enum Suit {C, D, H, S};
In Java, the enum type is a class and each value in the enum is a static class. So in the above example, Suit is a class and C, D, H, S are all classes. As classes, you can define methods for them, but we will not do that in this lab. Because they are static classes, you can compare values using the == operator instead of the .equals method. For example, if you have two variables of type Suit,
Suit s1 = Suit.S;
Suit s2 = Suit.S;
you can test that s1 and s2 both contain the same value with
if (s1 == s2)
  ...

Here are some useful methods with the enum value:
name: returns the name of a value. For example, s2.name() will return "S".
ordinal: returns a number indicating the location of the value in the enum, starting at 0. For example, s2.ordinal() will return 3 because Suit.S is the 4th value in the enum type.

Here is a useful methods with the enum set:
values: Returns an array containing all values of the enum. This is useful for accessing specific values. For example, Suit.values()[2] will return Suit.H and Suit.values().length will return the number of values in the Suit enum.

Using the values method, we can use a special type of for loop to iterate over all possible values. The loop is called a for-each loop:

for (Suit s : Suit.values())
The loop sets s to hold each value in Suit. This is equivalent to:
for (int i = 0; i < Suit.values().length; i++)
  Suit s = Suit.values()[i];

Your Task

You are to create two classes, Card and Deck. The Deck class should contain 3 enums. One for the suit of a card. I recommend using C, D, H, S, one for the face value of a card. I recommend using _A, _2, _3, ..., _K (note that you can not begin a value with a number), and one for the color of a card.

Create a static method in Deck that takes a suit and returns the color of the suit.

The Card class should have three private variables, one for the card suit, one for its face, and one boolean variable indicating whether the card is face up or down. Then create the following methods:

  1. A constructor that sets the suit and face value of the card.
  2. A method to get the suit of the card.
  3. A method to get the face value of the card.
  4. A method to get the color of the card. (Call the static method in deck to help you.)
  5. A method to get whether the card is face up or down.
  6. A method to set whether the card is face up or down.
  7. A toString method that returns "X" if the card is face down and something appropriate if the card is face up. You can probably call the name method for the suit and face and concatenate those two together.
Now add to the Deck class. Create a private variable that will contain a deck of cards. You should make the variable an ArrayList to take advantage of a built in shuffle method. Then create the following methods:
  1. A constructor that initializes the ArrayList to have the appropriate size (the product of the suit and face value enum lengths). Don't hard code in 52. The constructor should populate the ArrayList with one of each card using for loops.
  2. A method that prints the contents of the ArrayList. This method is not essential, but will help you see that your code is working.
  3. A method to shuffle the ArrayList. The Collections class contains a shuffle method that works on ArrayList objects.
  4. Three methods that will let you deal out the cards:

Finally, add a method to the Card class that will take another Card c and will indicate whether this card is in sequence with c. That is, whether it has the same suit as c and has a face value that is one less than c's.