Assignment 19
Task
Rewrite your Deck class to use an array, rather than an ArrayList .
Texbook: 7.1 - 7.3
Concepts: Arrays
Steps
Write a Deck class with exactly the same constructors and methods as described in A17. Use an array of PlayingCard objects in place of any ArrayList s.
Thus, you will probably need something like this:
private PlayingCard[] cards = new PlayingCard[52];
private int topCardIndex = 51;
Note that, because an array does not change length (size) once created, you need another variable to track the top card of the deck (or else the number of cards remaining, which is topCardIndex + 1 ). (This top card variable could start at 0 instead of 51, if your prefer.)
Note that, when shuffling, you may now more easily use the code from the Wikipedia Fisher-Yates page. However, note that you will not always be shuffling a full deck as that code assumes! (More specifically, that code shuffles array.length cards, where you'll want to only shuffle this.getSize() cards.) Your shuffle method should not reintroduce cards that have already been drawn from the deck, nor should your draw method return null (unless it is really empty).
Rename your Deck class to UsernameA19 to submit it. (Remember to rename the constructors, etc! I suggest you compile it again before submitting.) Make your class public. You do not need to include any other classes in the file, as Tamarin will be testing your class directly by calling its methods and will provide its own PlayingCard class that follows the A17 specifications.
What to Submit
Upload your UsernameA19.java file to Tamarin.
Grading [5 points]
- 1 - Compiles + Coding Standards
- Your program compiles successfully (no errors). Your code follows Java coding standards.
- 1 - Array
- Internally uses an array of
PlayingCard s to store the cards; does not use an ArrayList in any way.
- 1 - Constructors
- Contains the 2 requested constructors.
- 0.5 -
draw()
- Returns the top card; returns null if (and only if) the deck is empty.
- 0.5 -
getSize()
- Returns the number of cards left in the deck.
- 1 -
shuffle()
- Shuffles only the cards remaining in the deck (so does not shuffle already drawn cards back into the deck).
FAQs
- Demo code from class?
- Sorry it's a bit late, but here it is: StackOfInts.java
|