Exercise: A Card Class
Write a Java class that represents a playing card. A card has two attributes -
a suit and a rank. Hence, the Card class should have two instance
variables:
- suit - an integer (1 represents Spades, 2 represents Hearts,
3 represents Diamond, and 4 represents Clubs)
- rank - an integer representing the rank (1 represents Ace,
numbers 2 - 10 represent the number, 11 represents Jack, 12 represents
Queen, and 13 represents King)
Provide the
following public methods:
- Card() -- a parameterless constructor that randomly generates the
rank and suit of the card.
- int getValue() -- returns the point value of the card: 1 for Ace, 10 for
face cards, the number on the card for everything else.
- int getRank() -- returns an integer representing the card's rank (1..13).
Note that in this system, Ace is the lowest ranking card.
- String toString() -- returns a string representing the card as "<rank> of
<suit>", e.g., "3 of Diamonds" or "King of Spades". Method toString
should call other (private)
methods (which you will have to write) to
generate the name and suit strings; it can then just stick the results together
into the final string and return it.