JamesMadisonUniversity

Computer Science Department


CS 239 Lab 7: More Enumerated Types


Objectives:

The student will:

  • create several different enumerated types
  • use those types in a new object
  • use a testing program to unit test each component of the system.

Background:

Enumerated types allow the user to create a class of discrete constant values. This lab will explore the use of behaviors to accompany those values.

New Terms:

  • enum keyword

Materials:

There are no materials for this lab.  All work will be done from scratch. You will upload all of your files to the Blackboard Assignment for this lab.  There is no worksheet for this lab.

Acknowledgment

Lab by Nancy Harris  modified by Elizabeth Adams


1 Getting Ready:

Before going any further you should:

  1. Make a directory for this lab.
  2. Create a java file EnumTester.java which will contain a single main method that will be used to exercise each component of the lab and demonstrate that you have done that step directly.

2 Creating a Simple Enumerated Type:

This lab will help you to understand how to create, compile and use a simple enumerated type.

  1. A standard deck of cards contains cards of different suits and ranks.  Suits are a very simple enumerated type.  There are four suits in ascending order of precedence which will be clubs, diamonds, hearts and spades, the order used in a bridge game.  Spades is the highest.  Note that in English, the suits are in alphabetical order.  This is not true in other languages..
  2. Create an enum class, named Suit, which will contain objects representing these 4 values.
  3. Write a loop in the main method to print out the value of each of the four objects in this class preceded by the statement:  "Here are the four suits in this game."
  4. Write a comparison statement that will let you compare the value of clubs and hearts.  You should print the value of your comparison with an appropriate label.

 3 Using a More Sophisticated Enumerated Type:

This part of the lab will help you understand how you can overcome the shortcomings of simple enumerated types by adding behaviors.

  1. Ranks run from 2 through Ace (we are taking the simple case here where A always is a high value.)  In some games an Ace can be high or low depending on which does the player more good. Other games include jokers but this one doesn't.
  2. Create an enum class, Rank, which will contain objects of each of the 13 ranks.
  3. You will need to add two instance fields: bridgePoints and abbreviation  to the Rank type so that each object of this type will have a bridgePoints value and an abbreviation.  Points are integers and the abbreviation should be a  char type.  bridgePoints will hold the point value of the card for a bridge hand. The abbreviation is a single character representing each of the values for the ranks 2 through A (i.e. '2' ... 'A'). You will need to add a constructor that will build each of these objects.  Bridge points are assigned as follows: Ace = 4, King = 3, Queen = 2, Jack = 1, 10 - 2 = 0 points.  
  4. You will need to add a method to getBridgePoints
  5. You will need to add a method to get the abbreviation.  
  6. In your main method, you should test that each of the objects was built with the correct values.  Use each of the methods with each of the objects created (maybe in a loop?)

4 Putting these together:

We will now use these classes in building another class.

  1. Create a new class, Card, which will represent one standard playing card.  It has two attributes, a suit and a rank. 
  2. Create an explicit value constructor that takes in these two values.
  3. Create a toString method that will return a "card" in the form of: RANK of SUIT
  4. Create a getBridgePoints method to return the bridge point value of that card.
  5. In your main method, you should test that each of the objects was built with the correct values.  Use each of the methods and verify that your Card class works properly.

5 (Optional) but provides practice for both arrays of objects and manipulating classes:

Now make a Deck of Card objects.

  1. Your Deck should contain an array of 52 card objects.
  2. Your constructor should create a standard deck of all possible combinations of rank and suit.  (Think for each loops).
  3. Create a toString method that will print each card on its own line.
  4. Create a deal method that will randomly return one of the 52 cards in the deck.
  5. Test your deck by calling each of the methods to see that it will work properly with the other classes.

Updated 09/15/08 (esa)