CSE1030Z Lab 04

Thu Jan 31 2:30-4:00 PM

Introduction

The purpose of this lab is to implement an immutable class that uses static and non-static features.

There is one question in this lab, which should be submitted by the end of the lab session.

Question 1: Implement a Playing Card class using the multiton pattern

Implement a class that uses the multiton pattern to represent an immutable playing card from a standard 52 card deck. A playing card is identified by its suit (clubs, diamond, heart, spade) and its rank (two through ten, jack, queen, king, and ace). Both the suit and rank are represented with strings.

Your class must provide the API shown here.

Please start with the skeleton code shown below:

package cse1030;

/**
 * An immutable class representing a playing card from a standard 52 card deck.
 * A playing card is identified by its suit (clubs, diamond, heart, spade) and
 * its rank (two through ten, jack, queen, king, and ace). Both the suit and
 * rank are represented with strings.
 *  
 * The class is implemented as a multiton; there is only one
 * <code>PlayingCardM</code> object for each unique combination of rank and
 * suit.
 * 
 * @author 
 * 
 */
public final class PlayingCardM implements Comparable<PlayingCardM> {

  private static final String[] RANKS = { "2", "3", "4", "5", "6", "7", "8",
      "9", "10", "J", "Q", "K", "A" };
  private static final String[] SUITS = { "S", "C", "D", "H" };

  private static final Map<String, PlayingCardM> instances = 
      new HashMap<String, PlayingCardM>();

  private final String rank;
  private final String suit;
  
  

  public static void main(String[] args) {
    // generate a standard 52 card deck
    Set<String> ranks = PlayingCardM.getAllRanks();
    Set<String> suits = PlayingCardM.getAllSuits();
    for (String r : ranks) {
      for (String s : suits) {
        PlayingCardM card = PlayingCardM.getCard(r, s);
        System.out.println(card);
      }
    }
  }
}

Notice that PlayingCardM has three static attributes; one is the map that manages the instances, and the other two are arrays that hold the valid ranks and suits sorted in ascending order. The arrays are static because there is no need for every card to store a copy of the arrays. The class also has three static methods that you must implement.

A JUnit tester is available here.

Submit your class using the following command:

submit 1030Z L4R PlayingCardM.java