Files
oops/src/main/java/oving3/card/Card.java
T
2026-02-09 17:03:32 +01:00

70 lines
1.7 KiB
Java

package oving3.card;
/**
* The {@code Card} class is a so-called value-based class, which is coded in
* such a way that its
* objects cannot be modified after they are created. A {@code Card} object has
* a suit and a face.
*/
public class Card {
private char suit;
private int face;
/**
* The constructor of the {@code Card} class initializes the suit and face of
* the card with the
* first and second arguments, respectively.
*
* @param suit the suit of the card, one of {@code 'S'} (spades), {@code 'H'}
* (hearts),
* {@code 'D'} (diamonds), or {@code 'C'} (clubs)
* @param face the face of the card, an integer between {@code 1} (ace) and
* {@code 13} (king)
* (both inclusive)
* @throws IllegalArgumentException if the suit or face is illegal
*
* @see CardTest#testConstructor()
*/
public Card(char suit, int face) {
if (suit != 'S' && suit != 'H' && suit != 'D' && suit != 'C') {
throw new IllegalArgumentException("Illegal value for suit");
}
if (face < 1 || face > 13) {
throw new IllegalArgumentException("Illegal value for face");
}
this.suit = suit;
this.face = face;
}
/**
* @return the suit of the card
*/
public char getSuit() {
return suit;
}
/**
* @return the face of the card
*/
public int getFace() {
return face;
}
/**
* @return the value of the card of the form {@code <suit><face>}. For example,
* the ace of
* spades should return {@code "S1"}
*
* @see CardTest#testToString()
*/
@Override
public String toString() {
return suit + Integer.toString(face);
}
public static void main(String[] args) {
}
}