next up previous
Next: Merge Up: Assignment 1: The Deck Previous: Assignment 1: The Deck

The Deck class

On the class web page, at the following URL

http://rocky.colby.edu/cs231/code/hw1/Card.java
you will find a program named Card.java that creates a deck of playing cards and tests a few simple algorithms, like shuffling the deck, sorting the deck, and searching for a card in the deck (using a bisection search).

In this program, a deck is implemented as an array of Cards. For example, when we pass a ``deck'' as a parameter, the actual type of the parameter is Card[].

In Chapter 12 of the excellent textbook, ``How to think like a computer scientist,'' I present an alternative representation for a deck, an object type named Deck that contains an array of cards as an instance variable.

You should read Chapter 12 and understand the difference between these two representations before proceeding.

WARNING: The Chapter 12 I am referring to is Chapter 12 of the Second Edition of the book, which is significantly different from the version we used in CS115 last semester. I will hand out copies of the new Chapter 12 in class.

1.
Move Card.java into your development environment and compile and run it. It should print all the cards in the deck and then print a ``trace'' of two bisection searches, one successful, one not.

2.
Add a second class definition, Deck, to the program, either in a different file or along with Card. If you include them both in one file, then only one of them (the one that contains main) can be public.

You have some choices about how to organize the code, but here is what I did: I put both class definitions in one file named Deck.java and made Card a non-public class. Then I moved main into the Deck class.

3.
Of the methods currently in the Card class, decide which ones would be more appropriate as members of the new Deck class. Move them there, and modify them so that they work again.

4.
Look over the program and identify every place where an array of Cards is being used to represent a deck. Modify the program throughout so that it uses a Deck object instead. You can use printDeck in the text as an example.

It is probably a good idea to make this transformation one method at a time, and test the program after each change. On the other hand, if you are confident you know what you are doing, you can make most of the changes with search-and-replace commands.


next up previous
Next: Merge Up: Assignment 1: The Deck Previous: Assignment 1: The Deck
Allen B. Downey
1999-09-07