A6-Card Games

On This Page


Overview

The main points of this assignment are to:

  • Develop a class that represents a deck of cards
  • Work with arrays of objects and enumerated types
  • Develop searching and sorting algorithms for arrays or objects

Cards

The complete code for the Card class is available in Blackboard.

When describing a playing card, we refer to two properties of a card. The suit of a card refers to one of four possible sets of playing cards in a deck: clubs, diamonds, hearts, and spades. The rank of a card refers to the name of a card within a suit: ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, and king. To express the suit and rank, we use an enum for each property.

public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX,
    SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
}
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }

Enumerated types are discussed in the textbook on pages 380-387.

Arrays

Arrays are an important data structure in programming. With this assignment, you can become familiar with how arrays are used in Java.

One of the algorithms you will examine is bubble sort. The bubble sort algorithm cycles through the array from left to right (or top to bottom if you want to picture it that way) interchanging adjacent elements when the first is larger than the second. After each pass through the array the numbers will be getting closer to being in order. The sorting is finished when a cycle is completed where no two elements are out of order. The name of this sorting algorithm comes from the way smaller elements "bubble up" toward the left end (or top) of the array.

For an example of a bubble sort algorithm see http://www.solidware.com/sort/. The site shows both the source code (which you can adapt to this project) and an animated array of bubble sort at work.

Program Specifications

This assignment has several parts:

  1. Write a class named Deck that encapsulates all the information about a deck of playing cards.

    Download the Deck.java code in Blackboard to start your development.

  2. Your Deck class must use the Card class available in Blackboard and you may not change the Card class in any way.
  3. All member variables, except constant variables, of the Deck class must be declared private.
  4. The Deck class must contain the following public methods with the exact heading shown:
    /**
     * Constructs a standard 52-card deck where the cards
     * are initially in a sorted order.
     */
    public Deck() { }
    
    /**
     * Returns the count of the current number of cards in
     * the Deck.
     *
     * @return the count of the current number of cards in
     *  the deck.
     */
    public int getCount() { }
    
    /**
     * Returns a copy of the array of cards for testing
     * purposes.
     *
     * @return a copy of the array of cards.
     */
    public Card[] getCards() { }
    
    /**
     * Returns the current contents of the deck as a string.
     *
     * @return the current contents of the deck as a string.
     */
    public String toString() { }
    
    /**
     * Tests if a Card currently exists in this deck.
     *
     * @param card The card to search for in this Deck.
     * @return true if the card exists, otherwise returns
     * false.
     */
    public boolean cardExists(Card card) { }
    
    /**
     * Returns a single card from the top of this deck and
     * decrements the count of the cards remaining.
     *
     * @return a single card from the top of this deck or
     * null if no card remains.
     */
    public Card drawCard() { }
    
    /**
     * Shuffles the current cards of the deck into a random
     * order.
     */
    public void shuffle() { }
    
    /**
     * Sorts the current cards of the deck into suit major
     * order using bubble sort.
     *
     * Cards are ordered first by their suit value; cards of
     * the same suit are then ordered by their rank value.
     */
    public void bubbleSort() { }
    
    /**
     * Sorts the current cards of this deck into suit major
     * order.
     *
     * Cards are ordered first by their suit value; cards of
     * the same suit are then ordered by their rank value.
     */
    public void sort() {
        bubbleSort();
    }
    
  5. The sort() method must call the bubbleSort() method as shown unless you implement the extra credit for sorting.
  6. The toString() method must return a list of current cards of the deck in their current order.
  7. Your Deck class must pass all the tests of the Test Cases provided below.
  8. You must document and organize your code following the requirements in the How To Document and Organize Java Code. Your program must pass CheckStyle configured with the grade_checks.xml for full credit. Note that the CTC has this configured on their computers and that I have instructions for setting up TextPad to use CheckStyle.
  9. Do not use any package statements.
  10. Make an executable JAR of your program containing all the files described in the section of this document: What to Turn In. Set the Main-Class of the JAR file to the SimpleCardGame class.

Hints

  • You can work with the Rank and Suit using code like:
    Card.Rank r = Card.Rank.DEUCE;
    Card.Suit s = Card.Suit.CLUBS;
    System.out.println(r);
    System.out.println(s);
    

Test Cases

Testing is an important part of software development. Usually, programmers develop tests for each unit of code they produce. This is know as "unit testing" and allows the programmer to verify that the code they develop works correctly.

To assist you in developing your code, I provide tests for this assignment. These tests, known collectively as "test cases", help to make sure that your assignment meets its requirements. I will use these test cases (and perhaps others) to test your assignment, and you should too. If the test cases do not pass, then you will lose points for the assignment.

In Blackboard you will find the DeckTester class. Install the tests by downloading the file into the same directory as your .java source code files. Compile and run the test class as you would any other Java program. You may need to compile your source code before compiling and running the test code.

Extra Credit

The following are worth extra credit points:

  1. Implement another sorting algorithm into the sort() method of the Deck class. (1 point)
  2. Create a class Hand, in its own file, that represents a hand of cards. Use an array of Card objects to contain the cards in class Hand. Add a main() method to Hand that instantiates one or more Hand objects and shows that your Hand class works as expected. Add a method dealHand() to the Deck class that deals a hand of 7 cards with the following header: (3 points)
    /**
     * Draws a Hand of 7 cards from this Deck, reducing
     * the cards remaining.
     *
     * @return a Hand of 7 cards or null if there are not
     * enough cards left in the deck.
     */
    public Hand dealHand() { }
    
  3. Create a new improved card game that makes use of your Deck class. (1-3 points depending on the game)

Make certain that your README.txt file lists any extra credit attempted.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.

Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the course information page.

Program Compilation

  • 4: Source code compiles with no errors or warnings
  • 2: Source code compiles with warnings
  • 0: Does not compile

Functionality

  • 10: Demonstrates mastery of the assignment
    • Has extra features or demonstrates techniques beyond the assignment
    • Applies concepts from the lesson(s) appropriately
    • Meets all specifications (see above) with particularly elegant solutions
    • No errors encountered during operation
    • All test cases pass
  • 8: Has all the functionality expected of the assignment
    • Demonstrates many techniques from the lesson
    • Meets all specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
    • All test cases pass
  • 6: Has most of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets all but one of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
    • All but one test case passes
  • 4: Has some of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have more than 3 minor errors
    • At least 1/2 of all test cases pass
  • 2: Serious functional problems but shows some effort and understanding
    • Meets less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
    • Less than 1/2 of all test cases pass
  • 0: Does not execute or no specifications met

Program Style

  • 4: Code is well-documented
  • 3: Code has minor documentation errors
    • Has 1 documentation error
  • 2: Code has some documentation errors
    • Has 2-3 documentation errors
  • 1: Code has many documentation errors
    • Has more than 3 documentation errors
  • 0: No apparent attempt to document code

README.txt File

  • 2: README.txt file submitted with specified information included
  • 1: README.txt submitted but some information was not included
  • 0: No README.txt submitted

Total possible: $score, plus extra credit

What to Turn In

Submit your assignment following the instructions for homework. Include the following items for grading:

  1. README.txt file
  2. All Java source code and class files in a single executable JAR file

You must submit all the files needed for your assignment to compile and work correctly. Do not assume that the instructors has any files. Also, do not turn in files that are not part of your assignment, especially ones that do not compile. Your assignment must compile after removing all the .class files and running: javac *.java. In addition, your code must work as submitted.

Home | Blackboard | Announcements | Schedule | Room Policies | Course Info
Help | FAQ's | HowTo's | Links
Last Updated: May 09 2009 @11:38:50