Create a class that represents a playing card, with values for the four suits, p
ID: 3730623 • Letter: C
Question
Create a class that represents a playing card, with values for the four suits, plus none for the joker, and values for the thirteen card ranks, plus the joker. Make the default constructor set a default value for a card and demonstrate this by calling the default constructor to create a card. Construct a deck of cards and deal some cards and display them.
Create a new project for the assignment.
Create a new class called Card. The Constructors from superclass check box will create a constructor stub in the code.
Create an Enumerated Type in a new source file called Suit with the values NONE, CLUBS, HEARTS, SPADES, DIAMONDS.
Create an Enumerated Type in a new source file in the same package as the Card class, called Rank with the values JOKER, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE.
In the class Card,
Create a class attribute (field) called rank of type Rank.
Create a class attribute (field) called suit of type Suit.
Create getters and setters for these fields. Make the setters private, since we only want the constructor to set the card Suit and Rank (we would not want a programmer or user to set more than one ACE of CLUBS for instance).
Create a constructor that accepts a Suit and a Rank and use those values to set the attributes.
In the default constructor, initialize the suit and Rank attributes. Use the this constructor to call the constructor created in the previous step and initialize the rank and suit with a value of your choice (for example: Rank.ACE, Suit.CLUBS).
In the main method of the class, construct a default Card to demonstrate the default constructor initialization.
Also construct a few additional cards using the Suit and Rank constructor. Use local variables to hold the objects.
Using the toString() method from Enumerated Types, print the Suit and Rank of each Card created.
Create a Deck class that contains a field that is an array of fifty four cards (Containment).
Create a constructor on the Deck class that initializes the array of cards with the standard deck and two jokers, using for loops that iterate over the Suit and Rank values and set the array members with the appropriate Card constructor. Remember that the two Jokers only exist as 'NONE' suits and only the Jokers use the 'NONE' suit. There are many possible solutions for creating only two jokers. I used an iterator, left them out of the loop and added them in the beginning, individually. Use an if statement to exclude the jokers within a for each loop and add them before or after.
Create a toString() method in the Deck class, using another for loop to print all the Cards in the array by calling the toString() method on each card. This method “asks” the Card to print its rank and suit by only calling toString() on the Card object. There should be no reference to Rank or Suit anywhere in this method. Let Card's toString() method do the work (Delegation).
Create a main method in the Deck class that creates a Deck. Then print the deck using System.out.println(). This will call Deck's toString() method, which will call toString() on each Card, printing the entire deck. The main method in the Deck class is the method to set for the jar in this assignment.
As required for all assignments from now on
validate the proper functionality using the example answers below and review the Concepts and Ideas checklist
format the code
add a comment at the top of the file that reads @ author and your name
build the javadocs
build the jar file using the Deck class as the main method
Verify that the jar file contains source and class files and the MANIFEST.MF file contains a Main-Class. See the Resources section for Configuring Eclipse to view Jar files.
Run the application from the jar file, either from the command line using java -jar jarfilename or from Eclipse. See the Resources section for Configuring Eclipse to run Jar files.
submit the jar file in the Assignment section of BlackBoard under View/Complete Assignment for this assignment.
Example:
JOKER
JOKER
TWO of CLUBS
THREE of CLUBS
FOUR of CLUBS
FIVE of CLUBS
SIX of CLUBS
SEVEN of CLUBS
EIGHT of CLUBS
NINE of CLUBS
TEN of CLUBS
JACK of CLUBS
QUEEN of CLUBS
KING of CLUBS
ACE of CLUBS
TWO of HEARTS
THREE of HEARTS
FOUR of HEARTS
FIVE of HEARTS
SIX of HEARTS
SEVEN of HEARTS
EIGHT of HEARTS
NINE of HEARTS
TEN of HEARTS
JACK of HEARTS
QUEEN of HEARTS
KING of HEARTS
ACE of HEARTS
TWO of SPADES
THREE of SPADES
FOUR of SPADES
FIVE of SPADES
SIX of SPADES
SEVEN of SPADES
EIGHT of SPADES
NINE of SPADES
TEN of SPADES
JACK of SPADES
QUEEN of SPADES
KING of SPADES
ACE of SPADES
TWO of DIAMONDS
THREE of DIAMONDS
FOUR of DIAMONDS
FIVE of DIAMONDS
SIX of DIAMONDS
SEVEN of DIAMONDS
EIGHT of DIAMONDS
NINE of DIAMONDS
TEN of DIAMONDS
JACK of DIAMONDS
QUEEN of DIAMONDS
KING of DIAMONDS
ACE of DIAMONDS
This is just my example, feel free to be creative.
My hand contains:
ACE of CLUBS
KING of CLUBS
QUEEN of CLUBS
JACK of CLUBS
TEN of CLUBS
Explanation / Answer
public class Card {
public final static int SPADES = 0;
public final static int HEARTS = 1;
public final static int DIAMONDS = 2;
public final static int CLUBS = 3;
public final static int JOKER = 4;
public final static int ACE = 1;
public final static int JACK = 11;
public final static int QUEEN = 12;
public final static int KING = 13;
private final int suit;
private final int value;
public Card() {
suit = JOKER;
value = 1;
}
public Card(int theValue, int theSuit) {
if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS &&
theSuit != CLUBS && theSuit != JOKER)
throw new IllegalArgumentException("Illegal playing card suit");
if (theSuit != JOKER && (theValue < 1 || theValue > 13))
throw new IllegalArgumentException("Illegal playing card value");
value = theValue;
suit = theSuit;
}
public int getSuit() {
return suit;
}
public int getValue() {
return value;
}
public String getSuitAsString() {
switch ( suit ) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "Joker";
}
}
public String getValueAsString() {
if (suit == JOKER)
return "" + value;
else {
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";
default: return "King";
}
}
}
public String toString() {
if (suit == JOKER) {
if (value == 1)
return "Joker";
else
return "Joker #" + value;
}
else
return getValueAsString() + " of " + getSuitAsString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.