Create a program with a class called Card that represents a standard playing card. Each card has a suit and a face value. Create a driver class called DealCards that deals five random cards.
Helpful Tip: In the Card class you can use a switch or two
public class card {
public final static
// the 4 suits
int SPADES = 4,
HEARTS = 3,
DIAMONDS = 2,
CLUBS = 1;
public final static
int ACE = 1,
JACK = 11,
QUEEN = 12,
KING = 13;
private final int suit;
private final int value; // The value of this card, from 1 to 11.
public card (int theValue, int theSuit) {
value = theValue;
suit = theSuit;
}
public int getSuit() {
// Return the int that codes for this card's suit.
return suit;
}
public int getValue() {
// Return the int that codes for this card's value.
return value;
}
public String getSuitAsString() {
// Return a String representing the card's suit.
// (If the card's suit is invalid, "??" is returned.)
switch ( suit ) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "??";
}
}
public String getValueAsString() {
// Return a String representing the card's value.
// If the card's value is invalid, "??" is returned.
switch ( value ) {
case 1: return "Ace";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
default: return "??";
}
}
public String toString() {
// Return a String representation of this card, such as
// "10 of Hearts" or "Queen of Spades".
return getValueAsString() + " of " + getSuitAsString();
}
} // end class Card
import java.util.Random;
public class DealCard {
public static void main(String[] args) {
//private String selectRandomCard() {
String card;
System.out.println("This program displays a randomly chosen card");
card = new String();
//Card randomCard = cardDeck.getRandomCard();
System.out.println(String);
Random rnd = new Random();
int r;
r = rnd.nextInt(13)+1;
switch (r) {
case 1:
card = "Ace";
break;
case 2:
card = "2";
break;
case 3:
card = "3";
break;
case 4:
card = "4";
break;
case 5:
card = "5";
break;
case 6:
card = "6";
break;
case 7:
card = "7";
break;
case 8:
card = "8";
break;
case 9:
card = "9";
break;
case 10:
card = "10";
break;
case 11:
card = "Jack";
break;
case 12:
card = "Queen";
break;
default:
card = "King";
break;
}
return card;
}
/*
* Randomly select a card suit
* @ return The selected card suit
*/
private String selectRandomCardSuit() {
String cardSuit;
Random rnd = new Random();
int s;
s= rnd.nextInt(4) +1 ;
switch (s) {
case 1:
Aucun commentaire:
Enregistrer un commentaire