Write a class called card that contains instance data to


Assignment

Write a class called Card that contains instance data to represent a playing card with a face and a suit.

• Recall: A card can have one of 13 faces:
• Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen and King
• Recall: A card be part of 4 suits:
• Clubs, Diamonds, Hearts and Spades

Next, create a class called DeckofCards that stores 52 objects of the Card class. Include methods here to shuffle the deck, deal a card and determine if the deck has cards remaining.

• The shuffle method should assume that you have a full deck

Finally, Create a driver class called CardDriver, whose main method shuffles the deck, asks the user for the number of cards they want, and deals that number of cards, printing each card as it is dealt.

In the Card class you will need:

• Private members of the class to store:
• face value as an integer
• Integer face values can be 1 to 13 representing the strings Ace to King.
• suit value as an integer
• Integer suit values can be 1 to 4 representing the 4 suits.
• face name as a String and
• suit name as strings

• Optional: You can create constants to represent the faces and the suits if you want (and you can make these static!), but these constants are separate private members (i.e. they do not replace the face and suit variables of the class)

• A constructor that accepts the integer face values (1 -13) and the integer suit values (1 - 4) to represent a card

• Two private methods (support methods) to assign a string value to the card, based on the face and suit value (Example: if the face value is 4 and the suit value is 2 then that card should be "Four of Clubs".)

• The first private method should use a switch statement to assign a string value to each of the possible face values.

• Here, you should have 13 cases, no default is needed.

• The second private method should use a switch statement to assign a string value to each of the possible suit values.

• Here, you should have 4 cases, no default is needed.

• Your constructor should call both these support methods in its definition.

• A toString method to output the String representation of the card.

In the DeckofCards class you will need:

• Private members of the class to store:

• An array of 52 card objects

• A integer variable to keep track of the numberofCards in the deck

• A constructor that accepts no parameters. The constructor should:

• Instantiate the Card array to hold 52 cards

• Use a nested loop to instantiate each of the card objects in that array

• The first loop should control the face values

• The second loop should control the suit values

• The body of the loop should use the new operator for every position in the card array (and should pass an integer face and suit value to the constructor)

• A method to deal a card

• This method is a value returning method of type Card, and accepts no parameters

• If the number of cards in the deck is greater than zero, then return a card from the array

• Otherwise return null (this is needed to ensure that something is returned)

• A method to see if the deck has cards remaining

• This method returns true if the number of cards in the deck is greater than zero, and false otherwise. It accepts no parameters.

• A method to shuffle the deck

• This method shuffles by swapping random pairs of cards multiple times

• It accepts no parameters and returns no values

• It uses a random object to generate a values between 0 and 51 (representing the positions in the Card array)

• You will need a temp Card object as well as two local variables to store the generated values

• Pseudocode for this method is shown below:

// create random object here
Card temp;
int a, b;
for (9999 repetitions - for fun and better shuffling)
{
a = (randomly generated value)
b = (randomly generated value)
// swap algorithm shown below
// cards is array of Cards created by constructor
temp = cards[a];
cards[a] = cards[b];
cards[b] = temp;
}

In the CardDriver program, you will need:

• An object of type DeckofCards that creates a deck of Cards

• This object will be used to shuffle the deck in this program

• A Scanner object to get information the number of cards the user wants from the deck

• A local variable to store the number of cards the user wants

• A while loop to print out the cards

• The loop should run while the value in the loop control variable is less than the number of cards requested by the user AND there are still more cards in the deck.

• Use the boolean method from the DeckofCards class as part of the condition. In the event the user requests more than 52 cards, this method should stop dealing after 52 cards have been dealt.

• Use the deal method from the Deck of Cards class in the body of the loop.

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Write a class called card that contains instance data to
Reference No:- TGS02733990

Expected delivery within 24 Hours