Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Practice Questions on Enumerations. 1. Create an enum called Suit that contains constants for the four suits in a standard deck of cards (Clubs,
Practice Questions on Enumerations. 1. Create an enum called Suit that contains constants for the four suits in a standard deck of cards (Clubs, Diamonds, Hearts, and Spades). 2. Create an enum called Rank that contains constants for each of the 13 ranks of cards found in a standard deck of cards. For example: TWO, FIVE, JACK, KING, QUEEN, ACE. 3. a. Modify the Suit enum: Assign a number to each suit (1 to 4), a display name for each suit, and a unicode character code for the suit's symbol. The unicode values for each suite are in the table below. Make sure you add the appropriate data members, accessor methods, and a constructor. Unicode Values for Suit Symbols(store in char datatype) Clubs - '\u2663' Diamonds - '\u2662' Hearts- '\u2661' Spades- '\u2660' 3. b. Modify the Rank enum: Assign a numeric value to each card rank (Jacks are 11, Queens are 12, Kings are 13, and Aces are 1), a name for each card (e.g. "Two", "Five", "Jack", "Ace"), and a short name for each card (e.g "2", "5", "J", "A"). Add the appropriate data members, accessor methods, and a constructor. 3. c. Create a regular class called Card with the following members: Class: Card Data Members: - rank: Rank - suit: Suit Methods: + Card() + Card(rank:Rank, suit:Suit) + setRank(rank:Rank) : void + getRank() Rank +setSuit(suit:Suit) : void + getSuit() : Suit + toString() String +shortString(): String The mutator methods don't require validation since you can't pass anything invalid into the parameters: just assign the parameter to the data member. The toString() method should return a String representation of the card in the form: RankName of SuitName For example, "Jack of Clubs", "Two of Diamonds". The shortString() method should return a shorter String representation of the card in the form: ShortRankSuitSymbol For example: J or 2 3. d. Create a regular class called DeckOfCards with the following members: Class: DeckOfCards Data Members: - cards: Card[] Methods: + DeckOfCards() - init() : void + pickCard() Card + toString() String The array of cards should have 52 elements for Card objects. The DeckOfCards() constructor only needs to call the init() method. The init() method uses nested loops to load the cards array with one of each card in a standard card deck (e.g. one of each card rank with each suit). The pickCard() method generates a random array index and returns the Card object at that index in the cards array. The toString() method returns a String containing the list of Card objects in the cards array (see sample program and output below). Sample main() method code using the DeckOfCards object: DeckOfCards deck = new DeckOfCards(); System.out.println("My Deck of Cards:"); System.out.println(deck); System.out.println(" Random Card:"); Card c deck.pickCard(); = System.out.println(c.shortString());
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Based on the provided practice questions we need to define enumerations for card suits and ranks and create classes for a card and a deck of cards in ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started