We will build a Deck class which will let us build a deck of

We will build a Deck class which will let us build a deck of cards (52 Cards) consisting of 4 suits – Clubs, Diamonds, Hearts and Spades. Each suit will have – 2,3,4,5,6,7,8,9,10,11,12,13 and 14 whereby upon printing a Card, the 11 will be printed as Jack of …, the 12 will be printed as Queen of …, the 13 will be printed as King of … and the 14 will be printed as Ace of … They will store this Deck of Cards inside of an array. They will also add other methods that may need now or when they will be using this in the main program later.

All the suits should be in separate classes.

Solution

If we look for verbs in the description of a card game, we see that we can shuffle a deck and deal a card from a deck. This gives use us two candidates for instance methods in a Deck class: shuffle() and dealCard(). Cards can be added to and removed from hands. This gives two candidates for instance methods in a Hand class: addCard() and removeCard(). Cards are relatively passive things, but we at least need to be able to determine their suits and values. We will discover more instance methods as we go along.

First, we\'ll design the deck class in detail. When a deck of cards is first created, it contains 52 cards in some standard order. The Deck class will need a constructor to create a new deck. The constructor needs no parameters because any new deck is the same as any other. There will be an instance method called shuffle() that will rearrange the 52 cards into a random order. The dealCard() instance method will get the next card from the deck. This will be a function with a return type of Card, since the caller needs to know what card is being dealt. It has no parameters -- when you deal the next card from the deck, you don\'t provide any information to the deck; you just get the next card, whatever it is. What will happen if there are no more cards in the deck when itsdealCard() method is called? It should probably be considered an error to try to deal a card from an empty deck, so the deck can throw an exception in that case. But this raises another question: How will the rest of the program know whether the deck is empty? Of course, the program could keep track of how many cards it has used. But the deck itself should know how many cards it has left, so the program should just be able to ask the deck object. We can make this possible by specifying another instance method, cardsLeft(), that returns the number of cards remaining in the deck. This leads to a full specification of all the subroutines in the Deck class:

This is everything you need to know in order to use the Deck class. Of course, it doesn\'t tell us how to write the class. This has been an exercise in design, not in coding. You can look at the source code, Deck.java, if you want. It should not be a surprise that the class includes an array of Cards as an instance variable, but there are a few things you might not understand at this point. Of course, you can use the class in your programs as a black box, without understanding the implementation.

We can do a similar analysis for the Hand class. When a hand object is first created, it has no cards in it. An addCard() instance method will add a card to the hand. This method needs a parameter of type Card to specify which card is being added. For the removeCard() method, a parameter is needed to specify which card to remove. But should we specify the card itself (\"Remove the ace of spades\"), or should we specify the card by its position in the hand (\"Remove the third card in the hand\")? Actually, we don\'t have to decide, since we can allow for both options. We\'ll have two removeCard() instance methods, one with a parameter of type Card specifying the card to be removed and one with a parameter of type int specifying the position of the card in the hand. (Remember that you can have two methods in a class with the same name, provided they have different numbers or types of parameters.) Since a hand can contain a variable number of cards, it\'s convenient to be able to ask a hand object how many cards it contains. So, we need an instance method getCardCount() that returns the number of cards in the hand. When I play cards, I like to arrange the cards in my hand so that cards of the same value are next to each other. Since this is a generally useful thing to be able to do, we can provide instance methods for sorting the cards in the hand. Here is a full specification for a reusable Hand class:

We will build a Deck class which will let us build a deck of cards (52 Cards) consisting of 4 suits – Clubs, Diamonds, Hearts and Spades. Each suit will have –

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site