Question: Begin with the SuitsAndRanks class. This class will demonstrate using the Enum type. The completed class should contain: An Enum type named Rank going from
- Begin with the SuitsAndRanks class. This class will demonstrate using the Enum type. The completed class should contain:
- An Enum type named Rank going from TWO through ACE
- An Enum type named Suit with values CLUBS, DIAMONDS, HEARTS, SPADES
- A fetchSuit method that is passed in an integer and returns the corresponding Suit
- A fetchRank method that is passed in an integer and returns the corresponding Rank
- Use the .values method for Enum types to create an array of ranks and an array of suits
Starting SuitsAndRanks class - need help with the array portion:
public class SuitsAndRanks { /** * Provides the enumerated suits for playing cards in order of * * increasing importance for the game of bridge. */ //TODO: Create enum for Suit (i.e., CLUBS, DIAMONDS, HEARTS, SPADES) public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES };
/** * Provides the enumerated ranks for playing cards in ascending * order. */ // TODO: Create enum for Rank (ie TWO, THREE, ..., ACE) public enum Rank { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };
// TODO: Create a public static array of Suits named 'suits' // containing all the suits. public static Suit[] suits = Suit.values(); //TODO: Create a public static constant for the number of Suits. public static final int NUMSUITS = 4; // TODO: Create a public static array of Ranks named // 'ranks' containing all ranks. public static Rank[] ranks = Rank.values();
//TODO: Create a public static constant for the number of Ranks. public static final int NUMRANKS = 13;
/** * fetchSuit returns the Suit at a given index. * If the index is greater than the number of suits, * wrap the index back to zero using the mod operator. * * @param index Index into the 'suits' array * @return The Suit at that index */ //TODO: Create a public static method named 'fetchSuit' public static Suit fetchSuit(int index) { return suits[index % NUMSUITS]; } /** * fetchRank returns the Rank at a given index. * If the index is greater than the number of suits, * wrap the index back to zero using the mod operator. * * @param index Index into the 'ranks' array * @return The Rank at that index */ //TODO: Create a public static method named 'featchRank' public static Rank fetchRank(int index) { return ranks[index % NUMRANKS]; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
