YCP Logo Lab 8: Comparable, Sorting

Due: Friday, September 26th by 11:59 PM

Start by downloading CS201_Lab8.zip and importing it into Eclipse. You should see a project called CS201_Lab8.

Your Task

Part 1 - Define a Card class

Define two enum types called Suit and Rank. The Suit enumeration should contain values for the suits clubs (lowest suit), diamonds, hearts, and spades (highest suit). The Rank enumeration should contain values for each playing card rank - two (lowest rank) through ten, jack, queen, king, then ace (highest rank).

Define a class called Card. An instance of Card represents a playing card. It should have fields to store a Card's suit and rank, and a constructor to initialize them. Add getter methods to get a card's suit and rank.

Write JUnit tests to test the card class. You should create a number of Card objects with different suits and ranks, and test that the getter methods work on these objects.

Part 2 - Comparing Cards

Once you have Part 1 working, change the Card class to implement the java.util.Comparable interface. The Card class's compareTo method should compare first by suit, then by rank. So, a two of diamonds would compare as greater than an ace of clubs, because diamonds is a higher suit than clubs. Of course, three of diamonds would compare as greater than two of diamonds.

Add some JUnit tests to test comparing Card objects using the compareTo method.

Part 3 - Sorting Cards

Finally, demonstrate that you if you can compare cards, then an array of cards can be sorted using the java.util.Arrays.sort static method.

As one of the objects in your test fixture, create an array of Card objects. You can use the Card objects you've already created as element values. Make sure that the elements of the array are not already sorted.

Write a test method to sort the array. It should look something like this:

public void testSort() {

  // now verify that the elements of the array are in sorted order
  assertEquals(lowestCard, myArray[0]);
  assertEquals(secondLowestCard, myArray[1]);

  // etc...


When you are done, save the project (CS201_Lab8) to a zip file by right-clicking it and choosing

Export...->Archive File

Upload the saved zip file to the Marmoset server as lab8. The server URL is