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.wellesley.edu/cs230/code/hw01/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 5 of the textbook, 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 5 and understand the difference between these two representations before proceeding.

  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 in a file named Deck.java. Copy the instance variables and constructors from page 45, along with printDeck from page 46.
  3. Create a third class definition, Test that will contain main.
  4. 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.
  5. 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.

    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 Downey
Wed Sep 13 16:43:50 EDT 2000