Homework Arrays

In chapter 7, the Deitel & Deitel text gives example code for classes Card and DeckOfCards.

☑ Put these classes in a project, but do not use final for the instance vars in Card. Add to these classes all the methods that we know a real class should have. Also give DeckOfCards a method empty() that returns true if current has reached deck.length (that is, if it has dealt all the cards), false otherwise.

☑ Create a new class HandOfCards which has an array of Cards, and a firstEmpty.

☑ Give it a method addCard which adds a card to the hand if there is still room, but don't add anything if you are passed a null.

☑ You should have at least one parameterized constructor that lets you set up the max size of the hand.

☑ Add to the class a method checkFlush that returns true if a hand has at least 3 cards and all the cards in the hand are the same suit, false otherwise. (hint: once you know the face of the first card, they all have to match)

☑ Add to the class a method checkPair that returns true if the hand contains a pair (two cards with the same face) and false otherwise. (hint: any two cards could be a pair)

You can add other methods to these three classes that you find useful to do the following main, but remember that you shouldn't give direct access to the arrays from outside the class.

☑ In a main, create and shuffle a DeckOfCards. Ask the user how many players, and whether they want to play to a pair or a flush.

☑ Create an array of HandOfCards, the right size for the number of players.

☑ Repeatedly deal each of the hands one card at a time using addCard, but stop and announce a winner for the first hand to get a pair or a flush (depending on user choice), or stop and announce a draw if the deck runs out with no winner. [EC +10] If a hand has a pair or flush, stop right away and do not keep dealing, even if it is the middle of a round.

☑ Either way, print the final versions of all the hands.


hint: the main program is dealing randomly, but you can manually create cards and add them to a hand in a test harness to test that your methods work.