Lab 6: Linked Lists and Sorting Cards
CS 241: Data Structures, Fall 2007


Due Date: midnight on Thursday Oct 18

Goals for this assignment are to learn about:

Reading

Carefully read all of Chapter 6 before starting this assignment.

Part 1: Reverse

  1. You will need to enter the following code from the text: List.java, ListIterator.java, ListNode.java, LinkedList.java, and Predecessor.java. I suggest that you create a new project and copy over your "lists" package from the last lab. Add the linked list classes to it (it should already contain List.java). As you enter the code, look through it carefully so you understand what it is doing. Draw pictures if necessary to understand how things are linked together. Test the code by creating a linked list and applying the various functions.

  2. Write a reverse() method in your LinkedList.java class that reverses the order of the items in your list. No ListNodes should be created or destroyed in the process. Everything should be done by modifying references.

  3. Test that it works. This exercise is not to turn in but you should know how to do it! You will need your linked list classes for the next part of the lab.

Part 2: A simple Card Game (for ages 12 and under)

In this part of the lab you are to begin the implementation of the following game: Two players (you and the computer) are each dealt five cards. The remaining cards are placed face down in the center of the table. At each turn, a player picks two cards from the deck and discards two cards from their hand, trying to get five cards of the same suit in numeric sequence. The game continues with the players drawing and discarding until a player finally gets five the sequence. If the deck is used up and there is no winner, then the game is a tie (alternatively, you may take the discard pile and reshuffle it to continue with the game. This repeats until someone wins.)

  1. Getting Started: Create a new project and copy your "lists" package into it. Also copy over your "card" package containing the Card and Deck classes from the previous lab.

  2. Updating the Card class: As the cards are dealt to a player, the player must be able to insert the new card into the existing hand so that the cards are always sorted first by suit (shdc) and then by rank (A23456789TJQK). To do this, you need to define an ordering on the cards. That is, given any two cards, it should make sense to ask the question: Is (card1 < card2)? To do this, your Card class needs to implement the Comparable<Card> interface. Look up this interface in the Javadoc to see what method(s) you need to add to your Card class.

  3. Creating a Sorted Hand Class: The game above requires that each player maintains a hand of cards in sorted order. Note that the linked list class from the text has much of what you need for the Hand class except for the methods listed below. Therefore, your Hand class should extend LinkedList (with E set to Card). You then need to add the following methods to your Hand class:
  4. Set up some type of interface to show that your classes and their methods all work properly. For example, create a frame with 1) a textarea , 2) button to deal a card, 3) a comboBox to choose a card to discard when a second button is pressed. The program begins by creating, shuffling and printing the deck.

Part 3: Extra Credit (4 points) The Game

Complete the implementation of the game. Among other things, you will need to do the following:

Evaluation

By midnight on Thursday October 18, zip together the entire Card Game project and email the code as an attachment to gorr@willamette.edu. Make sure a working jar file is included. Place CS241 Lab 6 in the subject line.

By Tuesday October 23, demonstrate the code to the instructor.


[top]  [Schedule]  [Home]