YCP Logo Lab 9: Using Generic Containers and Algorithms

Start with your solution to Lab 8. You can download your solution from the server if you don't already have it in your workspace.

Rename the CS201_Lab8 project to CS201_Lab9 by right-clicking on it in the Solution Explorer, choosing Copy, and then choosing Edit->Paste from the menu bar. Enter CS201_Lab9 as the name of the new copy. Make your changes to this new project.

Your Task

(1) Modify your Card class so that rather than implementing the Comparable interface, it implements Comparable<Card>: instead of

public class Card implements Comparable {

use

public class Card implements Comparable<Card> {

Change the type of the compareTo method's parameter from Object to Card. You can now take out the type cast you used to convert the Object reference into a Card reference.

Run your JUnit tests again to make sure everything still works correctly.

(1.5) Add an equals method to the Card class. Add JUnit tests to CardTest to make sure it works properly.

(2) Implement the Deck class. It should look like this:

public class Deck {
        // fields

        public Deck() {
                // generate a full deck (52 cards)
        }

        public int getNumCards() {
                // return number of cards in the deck
        }

        public Card getCard(int i) {
                // return the i'th card in the deck
        }

        public Card drawCard() {
                // draw a Card from the top of the deck
                // and return a reference to it
        }

        public void shuffle() {
                // randomly shuffle the deck
                // use the Collections.shuffle() static method!
        }
}

Use an ArrayList<Card> (ArrayList of Cards) to store the cards.

The get method could use the get method of ArrayList<Card>.

The drawCard method could use the remove(int) method of ArrayList<Card>. (Use it to remove the last card in the list of cards, and return a reference to the removed card.)

Add a JUnit test class called DeckTest. It should contain tests for the getNumCards, drawCard, and Shuffle methods of the Deck class.

Hints

You can iterate through all of the elements of an enumeration by calling the enumeration's values static method, which returns an array. For example, this code snippet should be useful in the Deck constructor:

Suit[] allSuits = Suit.values();
Rank[] allRanks = Rank.values();

for (int j = 0; j < allSuits.length; j++) {
        for (int i = 0; i < allRanks.length; i++) {
                // use allSuits[j] and allRanks[i] to create a Card
        }
}

You can implement the Deck class's shuffle method by calling Collections.shuffle, passing the collection containing the Deck object's Cards as a parameter. In your JUnit test for shuffle, verify that at least some of the cards in the deck changed position.

Submitting

When you are done, save the project (CS201_Lab9) to a zip file by right-clicking it and choosing

Export...->Archive File

Upload the saved zip file to the Marmoset server as lab8. The server URL is

https://camel.ycp.edu:8443/