Lab 9

Using ADT List

In this lab, you will practice with the ArrayList and LinkedList classes as are available in the Java API. You will also need to implement a few methods for the LinkedList class as defined in your text. Please pay special attention to

-  The difference between the client (or external) view and the programmer’s (or internal) view of an ADT

-  Java APIs that deal with Lists

-  Java 1.5 features, such as generics and enum

Part 0: A Card Class Using Enumeration (enum)

We’ve seen in resources for Project 3 how to use enums to specify a finite number of values that can be used with a variable (such as chess piece names). In this lab we will see how enums can be used in defining ranks and suits for Poker cards. These card objects will then be used in the following parts to be organized with objects like Deck that use Lists.

1.  Read the CardRank.java and CardSuit.java files to see

  1. How enums are used to define a finite number of values?
  1. How to use enum values to compute abbreviation (or first letter) of card suits?
  1. How to use cardinal of CardRank enum to compute abbreviation of card ranks?
  1. How the cardinals of CardRank and CardSuit enums are used to compare cards?

2.  Write a simple CardTest class to play with cards: simply create two cards of your choice, print them out via System.out, and print out the result of comparing the two cards.

Part 1: Using Concrete List Classes in the Deck Class – A Client View

The Deck class is used to contain up to 52 card objects. Read the Deck.java file and consider the following questions?

3.  Why is a List object ideal for holding these Card objects? And why ArrayList was chosen?

4.  How is the values() method used to generate a whole deck of 52 Card objects?

5.  How to implements a shuffle() method to get a random permutation of the deck of cards? [Hint: you may use the sort() method as an example.]

6.  Use the notations used in your text to trace the program (a few times) to show how the insertCardInOrder method works.

7.  (Optional) Write a BridgeTest class to mimic dealing cards (from a deck) to four players in turn. Use a LinkedList to hold cards for each player, and insert the cards into each list in order. Print the four hands of cards to show how the cards are distributed. You may change the topic to test another game that you are familiar with.

Part 2: Implementing the LinkedList Class– A Developer’s View

The LinkedList class given in your text doesn’t implement all methods as specified in the java.util.List interface. Read the code closely and try to implement a few of them by yourself.

8.  How to implement the isEmpty() method?

9.  How to implement the size() method with the current design? How to improve the efficiency of this method?

10.  How to implement the add(Object o, int index) and remove(int index) methods? You may consider to use the ListIterator for help.

Deliverables

-  This answer sheet.

-  New code as specified.

-  Due date: 11/2/2010.