CMPU 101 - Lab 8

$Revision: 1.3 $

In this lab you will implement a class containing several methods, and then use JUnit to test those methods.

The Card class

The class you will implement represents a single playing card.  The name of the class will be (no surprise) Card.  The class will be instantiable, meaning that we will use it to create objects which are instances of the class.  That means that the class will have instance variables, a constructor, and instance methods.  It will also have several public int constants representing suits, and one static method, as described below.

You should define four public int constants called CLUB, DIAMOND, HEART, and SPADE.  Each constant should have a different integer value.  For example, you could represent the CLUB suit with the integer 0 by defining the CLUB constant like this:

public static final int CLUB = 0;

The constructor for Card should be public, and should take two parameters:

  1. An int specifying the suit
  2. An int specifying the rank of the card: 2-10 for number cards, 11 for Jack, 12 for Queen, 13 for King, and 14 for Ace

The class should define the following instance methods:

The class should implement a single static method:

Testing the Card class

Create a separate class called CardTest to test the Card class.  You can use the following skeleton to start this class:

import junit.framework.Assert;
import junit.framework.TestCase;

public class CardTest extends TestCase {
  // Define instance variables for test objects

  // Create test objects in setUp method
  protected void setUp() {

  // Define test methods

The skeleton defines three parts of the class: the test object instance variables, the setup method, and the test methods.

The test object instance variables are instance variables that store references to some number of objects that will be tested to make sure that they work correctly.

Here is an example test object instance variable:

private Card threeOfSpades;

As the name suggests, we can use this instance variable to store a reference to a Card representing the 3 of spades.

The setup method creates the test objects and assigns them to the test object instance variables.  Each time JUnit executes a test method to perform a test, it invokes the setUp method: this ensures that a freshly created set of test objects are created.

Since we have a test object instance variable called threeOfSpades, we can create it in setUp as follows:

threeOfSpades = new Card(Card.SPADE, 3);

The test methods perform tests by invoking methods and making sure they produce the expected results.  Each test method is a public void instance method whose name should begin with "test".  For example, we can check that the getSuit method is functioning properly by adding the following test method:

public void testGetSuitOnThreeOfSpades() {
  Assert.assertEquals(Card.SPADE, threeOfSpades.getSuit());

We could test the toString method on the same test object as follows:

public void testToStringOnThreeOfSpades() {
  Assert.assertEquals("3 of Spades", threeOfSpades.toString());

Running Tests

To run the test methods you have defined, click "Compile All" and then "Test" on the toolbar.  Hopefully, you will see a green bar indicating that all tests were successful.  If you see a red bar, that means that at least one test method failed, meaning that an assertion was triggered because an expected result value did not match the actual result value of a method.  This means that either

  1. The test found a bug in the method that was tested, or
  2. The test was not written correctly

You will have to check the code and determine which of these is the case, and then correct the problem.

Here is what your DrJava window might look like after running some tests in your test class:

Testing Criteria

You should add enough different test objects and test methods to ensure that each method in the Card class is tested by at least one test method.  When testing the toString method, add enough test methods to make sure that every suit is tested, and that at least one number card and one face card is tested.

When You Are Done

When you are done, demonstrate your tests to a lab coach or instructor.  All of your tests should succeed.

Submit your Java source files (both "" and "") using the CS 101 Submission WebsiteMake sure you submit the Java source file: the filename should end in ".java", not ".java~" or ".class".