This commit is contained in:
2026-02-09 17:03:32 +01:00
parent af8cbf6768
commit 8e08785db0
6 changed files with 187 additions and 49 deletions
+52 -47
View File
@@ -8,57 +8,62 @@ package oving3.card;
*/
public class Card {
// TODO: Add fields here
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) {
// TODO: Implement this constructor
}
/**
* 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() {
// TODO: Implement this method
return '\0';
}
/**
* @return the suit of the card
*/
public char getSuit() {
return suit;
}
/**
* @return the face of the card
*/
public int getFace() {
// TODO: Implement this method
return 0;
}
/**
* @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() {
// TODO: Implement this method
return null;
}
/**
* @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) {
public static void main(String[] args) {
}
}
}
+43
View File
@@ -0,0 +1,43 @@
package oving3.card;
import java.util.ArrayList;
public class CardDeck {
private ArrayList<Card> deck = new ArrayList<>();
CardDeck(int n) {
if (n < 0 || n > 13) {
throw new IllegalArgumentException("value of n needs to be >= 1 and <= 13");
}
char[] suits = { 'S', 'H', 'D', 'C' };
for (char suit : suits) {
for (int face = 1; face <= n; face++) {
deck.add(new Card(suit, face));
}
}
}
int getCardCount() {
return deck.size();
}
Card getCard(int n) {
return new Card('S', 1);
}
void shufflePerfectly() {
int n = deck.size();
if (n < 2) {
return;
}
ArrayList<Card> shuffledDeck = new ArrayList<>();
for (int i = 0; i < n / 2; i++) {
shuffledDeck.add(deck.get(i));
shuffledDeck.add(deck.get(n / 2 + i));
}
if (n % 2 != 0) {
shuffledDeck.add(deck.get(n - 1));
}
deck = shuffledDeck;
}
}