Wed Jan 30 2:30-4:00 PM
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.
Implement a class that represents 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. * * * @author * */ public final class PlayingCard implements Comparable<PlayingCard> { 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 final String rank; private final String suit; public static void main(String[] args) { // generate a standard 52 card deck Set<String> ranks = PlayingCard.getAllRanks(); Set<String> suits = PlayingCard.getAllSuits(); for (String r : ranks) { for (String s : suits) { PlayingCard card = new PlayingCard(r, s); System.out.println(card); } } } }
Notice that PlayingCard
has two static
attributes
that 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 two static
methods that you must implement.
hashCode
and equals
are acceptablejava.util.Arrays
and its method
asList
in the constructor, compareTo
, getAllRanks
,
and getAllSuits
PlayingCard.compareTo
carefully and make sure that
your implementation satisfies the API contractA JUnit tester is available here.
Submit your class using the following command:
submit 1030Z L4W PlayingCard.java