![]() |
Assignment 13TaskGet more practice with ArrayLists and breaking problems down into classes by modeling a deck of playing cards. StepsYour finished program should create a full deck of standard playing cards. That is, 52 cards, Ace through King, in four suits: ♦ ♥ ♠ ♣. A new deck should be in shuffled order. No card should be duplicated within a deck. Use an ArrayList to hold your cards. Your program should then allow the user to draw as many cards as she'd like from this deck. Each card drawn should be displayed one per line. After each draw, your program should state how many cards remain in the deck. If the next draw would exceed the number of cards left the current deck, a new shuffled deck should be created so that drawing may continue normally. The program should indicate when a new deck has been "opened"/created. Sample OutputThis is your digital deck of playing cards! A new deck has already been opened and shuffled for you. [52 cards left in deck] How many cards to draw (0 to quit)? 1 10 of Spades [51 cards left in deck] How many cards to draw (0 to quit)? 7 Ace of Spades Queen of Diamonds 8 of Clubs Queen of Clubs 2 of Clubs 3 of Diamonds 3 of Clubs [44 cards left in deck] How many cards to draw (0 to quit)? 2 10 of Diamonds King of Spades [42 cards left in deck] How many cards to draw (0 to quit)? 45 King of Clubs 6 of Hearts 3 of Hearts Ace of Hearts 5 of Diamonds 6 of Diamonds King of Diamonds 4 of Clubs 10 of Clubs 6 of Spades 9 of Spades 2 of Spades 2 of Hearts 5 of Spades 8 of Spades 6 of Clubs 4 of Spades 7 of Clubs King of Hearts 9 of Diamonds 7 of Spades 3 of Spades 7 of Hearts 7 of Diamonds Queen of Hearts Jack of Diamonds Jack of Spades 9 of Hearts 4 of Diamonds Queen of Spades Jack of Clubs Jack of Hearts 9 of Clubs 8 of Diamonds 4 of Hearts 10 of Hearts 5 of Clubs Ace of Diamonds 8 of Hearts 5 of Hearts 2 of Diamonds Ace of Clubs [--Opened a new deck--] 3 of Diamonds 6 of Diamonds 3 of Spades [49 cards left in deck] How many cards to draw (0 to quit)? 0 DesignWe discussed in lab different ways one might go about implementing this program. You may implement it however you like as long as it meets the requirements above. One option is to use three classes with the following details:
We also discussed how, for this particular program, all we need for a PlayerCard is its String representation. So we could skip this class and just use an ArrayList<String>. Similarly, we could move the behavior of the Deck class into Assignment13.java as well, and just do everything in main. However, at least creating and shuffling a new deck should probably be in its own method, since we have to do it multiple times. Shuffling
If you need a good shuffling algorithm, check out Fisher-Yates. Here's my version of it in a /** * Shuffles the cards in this deck using the Fisher-Yates algorithm. */ public void shuffle() { PlayingCard temp; int tempIndex; for (int i = cards.size() - 1; i >= 0; i--){ tempIndex = (int) (Math.random() * (i + 1)); if (tempIndex != i){ //swap if different elements temp = cards.get(i); cards.set(i, cards.get(tempIndex)); cards.set(tempIndex, temp); } } } What to submit
Attach all the classes needed to run your program to an email. The class containing your main method should be named FAQs
GradingOut of 10 points:
|
~ztomasze Index :
TA Details: ICS111:
Assignment 13 http://www2.hawaii.edu/~ztomasze |
Last Edited: 22 May 2008 ©2008 by Z. Tomaszewski. |