(JAVA) Game Basics: Cards and Hands 1) The Card class: A Public enum Type: Defin
ID: 3916142 • Letter: #
Question
(JAVA)
Game Basics: Cards and Hands
1) The Card class:
A Public enum Type:
Define the Suit enum, { clubs, diamonds, hearts, spades }, inside the Card class.
Private Member Data
Include three members:
char value
Suit suit;
boolean errorFlag;
Public Methods:
Card(char value, Suit suit) - The constructor should call the proper mutator(s). Overload this to cope with a client that wants to instantiate without parameters and use 'A' and 'spades' as the default value and suit when not supplied. Provide at least two constructors -- no parameters and all parameters -- or more if you wish. Because we have the errorFlag member, the constuctor (via the mutator), can set that member when it gets bad data; it does not have to assign default values upon receipt of bad data. This is a new technique for us. Again, default card (no parameters passed) is the ('A', spades).
String toString() - a stringizer that the client can use prior to displaying the card. It provides a clean representation of the card. If errorFlag == true, it should return correspondingly reasonable reflection of this fact (something like "[ invalid ]" rather than a suit and value).
boolean set(char value, Suit suit) - a mutator that accepts the legal values established in the earlier section. When bad values are passed, errorFlag is set to true and other values can be left in any state (even partially set). If good values are passed, they are stored and errorFlag is set to false. Make use of the private helper, listed below.
No mutator for errorFlag - that would not make sense.
Accessors for suit, value and errorFlag.
boolean equals(Card card) - returns true if all the fields (members) are identical and false, otherwise.
Private Methods
boolean static isValid(char value, Suit suit) - a static helper method that returns true or false, depending on the legality of the parameters. Note that, although it may be impossible for suit to be illegal (due to itsenum-ness), we pass it, anyway, in anticipation of possible changes to the type from enum to, say, char or int, someday. We only need to test value, at this time.
Note: we don't need individual mutators for value or suit since they would not be needed for this particular class.
2) The Hand Class:
Static Class Constants
Define a public int value like MAX_CARDS and set it to something like 30 or 50 so a runaway program can't try to create a monster array.
Private Member Data
Public Methods
Hand() - a default constructor.
void resetHand() - remove all cards from the hand (in the simplest way).
boolean takeCard(Card card) - adds a card to the next available position in the myCards array if the parameter is an error-free Card object and if there is room in the Hand object for another card (according to MAX_CARDS). It returns false if the Hand was full, and true otherwise, even if card was an invalid (error-containing) card. So, if card is invalid but there would have been room for it, the method will return true, even though it did not add card to the hand. This is an object copy, not a reference copy, since the source of the Card might destroy or change its data after our Hand gets it -- we want our local data to be exactly as it was when we received it.
Card playCard() - returns and removes (effectively, not physically) the card in the top occupied position of the array.
String toString() - a stringizer that the client can use prior to displaying the entire hand.
Accessor for numCards.
Card inspectCard(int k) - Accessor for an individual card. Returns a card with errorFlag = true if k is bad.
Recommended test of Hand class
Create between two and five explicit Card objects and one Hand object. Use takeCard() on these few cards (resulting in many, unavoidable "duplicates" in the hand) in a loop to populate the hand until the maximum allowable cards is met (use this criterion to end the loop). Display the hand using toString(). Next, play each card in a loop, until the hand is empty. Display the card played as it is played, and finally, display the (now empty) hand, verifying that no cards remain. At some point in your program, testinspectCard() with both legal and illegal int arguments.
Your Clients and Runs
Since there are two phases, you'll want to have two different test main()s (or else one larger test main() that has two unrelated parts, phase 1 followed by phase 2). So supply both main()s and label which is which (phase 1 or phase 2) and two runs, or one large run clearly labeling the separation between phase 1 and phase 2.
Example Test Run of Hand Class:
Explanation / Answer
Please find the code for hand and Card class, i would request you to post another question for test class, as this is a very long question and it took quite a lot of time to come up with above 2 classes. Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.