CS210: Intermediate Computing/Data Structures (Java)

Lab #7

Purpose

This lab gives you experience developing a method that implements a radix sort by two different fields for Card objects in a Deck object using a java.util.Queue interface (with a LinkedList implementing class).  You will sort the Cards in the Deck by Suit and Value.  (For convenience, the deck of cards is smaller than the normal card deck and only contains Ace through Ten for each of the four suits (Spades, Hearts, Diamonds, and Clubs) which is 20 cards instead of the normal 52 cards.)

Pre-Lab Assignment

Prior to the lab session, do the following to get started:

Download, save, and unzip the file Lab7.zip on your removable media (floppy disk, Zip disk, or memory stick) creating a directory named Lab7.

Open the Dr Java Application on your PC. 

Create a new Dr Java project in the Lab7 directory by selecting the menu Project -> New… naming the project file Lab7 and saving it in your new Lab7 directory.  (Dr Java automatically opens an Untitled file in the editor window which you will ignore.)

Using the menu File-> Open, open the files: Card.java  and Deck.java.  Dr Java will automatically include these source files in the Project.  Use the menu Project -> Save to save the updated project file. 

Use javadoc to see the javadoc html document for these classes if that is helpful.  Otherwise, read the javadoc comment blocks in the source files.  

Lab Activities

You need to design and add code for the sort method in the Deck class where indicated.  The main method in the Deck class can be used for the testing.  The main method in the Card class can be ignored as it was only included for testing of the Card class code prior to the Deck class code being available.  Note: This is a way (different from JUnit framework) to document the unit test code for each class and makes it available for regression testing.  Only one main method is the main method for the program as a whole (in this case the main method in the Deck class), but each class file can have a main method containing unit test code for testing that class by itself.

 

The Deck constructor builds and populates an array of Card objects with 20 cards in pseudorandom order.  The toString method prints out the cards in order.  The Deck main method instantiates a deck, prints the deck before sorting it, sorts the deck, and then prints it again. 

 

In the radix sort method, you must sort by the least significant field first (value) and then by the most significant field (suit).  In the code for each sort, you must instantiate an array of Queue / LinkedList objects with a size suitable for one queue for each value or suit (There are constants available in the Card class).  Move the cards from the array to the appropriate queue for its value or suit.  Then, move the cards back to the array by emptying each queue in sequence.  After the second sort, just return leaving the Card objects in the final (hopefully sorted) order. 

 

A sample run of the finished program is shown here:

 

> java Deck

Ten of Spades

Jack of Diamonds

Ten of Hearts

Ten of Diamonds

Queen of Hearts

Queen of Spades

King of Spades

Queen of Diamonds

Ace of Hearts

Jack of Spades

Ace of Clubs

Ace of Spades

King of Diamonds

King of Clubs

Queen of Clubs

King of Hearts

Ace of Diamonds

Jack of Clubs

Jack of Hearts

Ten of Clubs

 

Ace of Spades

King of Spades

Queen of Spades

Jack of Spades

Ten of Spades

Ace of Hearts

King of Hearts

Queen of Hearts

Jack of Hearts

Ten of Hearts

Ace of Diamonds

King of Diamonds

Queen of Diamonds

Jack of Diamonds

Ten of Diamonds

Ace of Clubs

King of Clubs

Queen of Clubs

Jack of Clubs

Ten of Clubs

 

> 

Report

In your report, explain the difference between the definition of the Card class in this lab and the definition of a class that could be sorted using one of the other general purpose sorting methods that we are currently studying. 

 

Attach a sample run of the output from your code.