January 21, 2019 / Classwork 1 / Due: ______
Purpose
The purpose of this exercise is to get some practice with topics we have discussed so far includingAstah*,enumerated types, randomnumbers, and arrays. The exercise is in twoparts. Please complete Part I and test it as indicated before starting on Part II.
Part I – Create and Test a Playing Card Class using Enumerated Types
Face and Suit Enumerated Types
Define enumeratedtypes named Face and Suit that represent the faces (Ace, Seven, Queen, etc.) and suits (Clubs, Diamonds, Hearts, and Spades) found in an ordinary deck of playing cards. The definitions for Face and Suit should be placed in separate files named Face.java and Suit.java.
Card Class
A class named Card is to be created. This class models a single card out of an ordinary deck of playing cards. It should have two private attributes:
- face
- suit
whose values come from enumeratedtypes that you defined above named Face and Suit.
The Card class should have threeconstructors:
- The default constructor a “default” card– you may choose any face and any suit as your default
- A copyconstructor with a header of public Card (Card existingCard). This constructor initializes a new Card as a duplicate of an existing card (the parameter). The body of this method should simply copy the attributes of existingCard into their counterparts in the Card being initialized.
- The parameterizedconstructor takes an integerparametern and converts it to appropriate subscripts using the mod (%) operator. For the parameterizedconstructor, appropriate values for the suit and face class attributes should be generated from constructor’s integer parametern using the array returned from the values method in the enumerated classes.
The Card class should also have a toString method that returns a String representing an appropriate displayable phrase such as “the Jack of Diamonds” or “the Deuce of Hearts”.
Driver Class
A driver class is to be created to demonstrate the functionality of the Card class.
- In it, create and display a “defaultCard” using the defaultconstructor.
- Then, in a loop, prompt the user to type an integer. Take the user’s integer and create a Card object using the parameterizedconstructor. Display the Card. Repeat until the user indicates a desire to stop.
- Finally, in the driver class, create a privatestaticmethod named showDeck that uses a for-loop to create and display all 52 Card objects that are possible. Invoke this method at the end the main method.
UML Diagram
Before coding, use Astah* to draw a UMLdiagram showing these classes and enumerated types.
Implement Solution
Use Eclipse to implement the classes diagrammed in Astah*. Test the solution appropriately before continuing to Part II. Do not begin Part II until you have thoroughlytested your solution to Part I.
Part II – Add a Deck Class to Part I and Test it
Create a Deck class representing a deck of cards. It should have at least two private attributes: an array of 52 Card objects and an integer representing the position of the next (as yet) undealt Card in the array. This integer will be initialized to 0 in the constructors and in the shuffle method.
The Deck Class
This class should have a defaultconstructor and a copyconstructor (that creates a new deck that is a duplicate of an existingdeck using a deepcopy operation). It should also have at least the following public methods, but it may have additional public or private methods as well.
- shuffle ( ) – rearrange the Cardsin the Deck’sarray of Cardsinto a randomorder. This method shuffles the array of Card objects by looping through the array of Card objects one position at a time and exchanging (see slides62-63 of the array lecture) the Card in that position with the Card in a random position (as determined by a Random number between 0 and 51).
To further randomize the order of the cards, one may repeat the full loop additional times (equivalent to shuffling a deck of cards by hand repeatedly).
- dealACard( ) – returns the next undealtCard in the array (after verifying that there is at least one left). Return a nullreference if there are no remaining undealtCards. See hint.
- dealAHand(int n) – returns an array of Card objects containing the next nundealtCards in the Deck. If there are fewer than n undealt Cards in the Deck, return a null reference. Verify that n is between 1 and 52. This method fills the array to be returned with Cards obtained by invoking dealACard n times in a forloop.
- toString ( ) – return a String containing the formatted String representation of each Card (see Card’stoString method) in the Deck. There should be one Card per line in the returned String. This method could be used in the driver to display the entire Deck.
The New Driver
Replace the driver from Part I with adriver that creates and displays an entire Deck of Cards, shuffles the Deck and displays it again, creates another Deck as a copy of the first Deck, and deals and displays two hands of 7 Cards each from that second Deck.
UML Diagram
Before coding, modify your Astah* diagram from Part I to include the new Deck class and the replacementDriver class.
Implement Solution
Use Eclipse to implement the classes diagrammed in Astah*. Test the solution appropriately.
Deliverable
Turn in the five.java files from parts I and II (the driver submitted should be the one from part II). Include the UMLdiagram as a part of the zipped file submitted. Follow submission instructions in the CourseFacts on the website.
Hint
/ Array of Cards in Deck ClassnextCard→ / Card 0
Card 1
Card 2
Card 3
Card 4
Card 5
Card 6
Card 7
Card 8
Card 9
.
.
.
Card 49
Card 50
Card 51
Classwork 1Page 1