next up previous
Next: Compare cards Up: Assignment 10: Homemade Objects Previous: Calculating GCDs

The Card class

1.
Create a new Java project called Card; the Card class will define a new type of object that will represent a playing card. It will have two instance variables, rank and suit, that represent the rank and suit of the card.

There are several choices for the type of rank and suit. They could be Strings or ints, or even doubles. For this project we will make them ints, so that they are easy to compare (among other things).

2.
Write two constructors for the Card class. One should take no arguments and initialize both instance variables to 0. The other should take the rank and suit as arguments.

3.
Write a printCard method. To do this, we need to choose a mapping from integers onto suits, and from integers onto ranks. For example, a sensible mapping for suits is:

\begin{eqnarray*}0 & \mapsto & Clubs \\
1 & \mapsto & Diamonds \\
2 & \mapsto & Hearts \\
3 & \mapsto & Spades
\end{eqnarray*}


where the symbol $\mapsto$ is pronounced ``maps to.'' This mapping makes sense because the suits are in increasing order, so a larger integer represents a superior suit.

This week, you will probably need to use a chained conditional to print the suits and ranks, but next week we will see a better way to do it.

4.
Write a main method that creates a new card using either constructor, and prints the card using printCard.


next up previous
Next: Compare cards Up: Assignment 10: Homemade Objects Previous: Calculating GCDs
Allen B. Downey
1999-04-14