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];
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:
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.