I need a Driver class for running the Deck Class. The output of the Driver and C
ID: 3834482 • Letter: I
Question
I need a Driver class for running the Deck Class. The output of the Driver and Codes for the Deck Class are given.
Deck: A class that represents the source of the cards for dealing and, as the game progresses, the place from which players can receive new cards (say, as they pick cards "from the deck" or when future hands are to be dealt from the same deck). Recall this picture, which relates the Deck to the various Hands that it creates through the process called "dealing":
Let's deconstruct the meaning of this important class.
y the operating system, is a resource we do not abuse).
Example Test Run of Card Class
//Deck Class:
class Card
{
// type and constants
public enum Suit { clubs, diamonds, hearts, spades }
static char DEFAULT_VAL = 'A';
static Suit DEFAULT_SUIT = Suit.spades;
// private data
private char value;
private Suit suit;
boolean errorFlag;
// 4 overloaded constructors
public Card(char value, Suit suit)
{ // because mutator sets errorFlag, we don't have to test
set(value, suit);
}
public Card(char value)
{
this(value, Suit.spades);
}
public Card()
{
this(DEFAULT_VAL, DEFAULT_SUIT);
}
// copy constructor
public Card(Card card)
{
this(card.value, card.suit);
}
// mutators
public boolean set(char value, Suit suit)
{
char upVal; // for upcasing char
// convert to uppercase to simplify
upVal = Character.toUpperCase(value);
if ( !isValid(upVal, suit))
{
errorFlag = true;
return false;
}
// else implied
errorFlag = false;
this.value = upVal;
this.suit = suit;
return true;
}
// accessors
public char getVal()
{
return value;
}
public Suit getSuit()
{
return suit;
}
public boolean getErrorFlag()
{
return errorFlag;
}
// stringizer
public String toString()
{
String retVal;
if (errorFlag)
return "** illegal **";
// else implied
retVal = String.valueOf(value);
retVal += " of ";
retVal += String.valueOf(suit);
return retVal;
}
// helper
private static boolean isValid(char value, Suit suit)
{
// don't need to test suit (enum), but leave in for clarity
char upVal; // string to hold the 1-char value
String legalVals = "23456789TJQKA";
// convert to uppercase to simplify (need #include )
upVal = Character.toUpperCase(value);
// check for validity
if ( legalVals.indexOf(upVal) >= 0 )
return true;
else
return false;
}
public boolean equals(Card card)
{
if (this.value != card.value)
return false;
if (this.suit != card.suit)
return false;
if (this.errorFlag != card.errorFlag)
return false;
return true;
}
}
/* --------------------------Driver Run Test-------------------------------
A of spades / K of spades / Q of spades / J of spades / T of spades / 9 of
spades / 8 of spades / 7 of spades / 6 of spades / 5 of spades / 4 of spade
s / 3 of spades / 2 of spades / A of hearts / K of hearts / Q of hearts /
J of hearts / T of hearts / 9 of hearts / 8 of hearts / 7 of hearts / 6 of
hearts / 5 of hearts / 4 of hearts / 3 of hearts / 2 of hearts / A of diamo
nds / K of diamonds / Q of diamonds / J of diamonds / T of diamonds / 9 of
diamonds / 8 of diamonds / 7 of diamonds / 6 of diamonds / 5 of diamonds /
4 of diamonds / 3 of diamonds / 2 of diamonds / A of clubs / K of clubs / Q
of clubs / J of clubs / T of clubs / 9 of clubs / 8 of clubs / 7 of clubs
/ 6 of clubs / 5 of clubs / 4 of clubs / 3 of clubs / 2 of clubs /
K of spades / 9 of clubs / Q of hearts / T of diamonds / 9 of spades / 5 of hear
ts / 2 of clubs / A of hearts / J of hearts / K of clubs / T of hearts / 4 of sp
ades / 7 of hearts / 3 of spades / Q of spades / A of clubs / 8 of spades / 5 of
clubs / T of clubs / J of spades / 2 of spades / 6 of diamonds / 2 of diamonds
/ 8 of diamonds / 4 of hearts / 2 of hearts / 6 of hearts / 5 of spades / 8 of h
earts / 7 of diamonds / 7 of spades / Q of diamonds / K of hearts / J of diamond
s / T of spades / 8 of clubs / 3 of diamonds / J of clubs / 6 of spades / 4 of c
lubs / K of diamonds / 4 of diamonds / 3 of clubs / A of spades / 9 of hearts /
Q of clubs / A of diamonds / 6 of clubs / 5 of diamonds / 7 of clubs / 3 of hear
ts / 9 of diamonds /
A of spades / K of spades / Q of spades / J of spades / T of spades / 9 of
spades / 8 of spades / 7 of spades / 6 of spades / 5 of spades / 4 of spade
s / 3 of spades / 2 of spades / A of hearts / K of hearts / Q of hearts /
J of hearts / T of hearts / 9 of hearts / 8 of hearts / 7 of hearts / 6 of
hearts / 5 of hearts / 4 of hearts / 3 of hearts / 2 of hearts / A of diamo
nds / K of diamonds / Q of diamonds / J of diamonds / T of diamonds / 9 of
diamonds / 8 of diamonds / 7 of diamonds / 6 of diamonds / 5 of diamonds /
4 of diamonds / 3 of diamonds / 2 of diamonds / A of clubs / K of clubs / Q
of clubs / J of clubs / T of clubs / 9 of clubs / 8 of clubs / 7 of clubs
/ 6 of clubs / 5 of clubs / 4 of clubs / 3 of clubs / 2 of clubs / A of sp
ades / K of spades / Q of spades / J of spades / T of spades / 9 of spades
/ 8 of spades / 7 of spades / 6 of spades / 5 of spades / 4 of spades / 3
of spades / 2 of spades / A of hearts / K of hearts / Q of hearts / J of he
arts / T of hearts / 9 of hearts / 8 of hearts / 7 of hearts / 6 of hearts
/ 5 of hearts / 4 of hearts / 3 of hearts / 2 of hearts / A of diamonds /
K of diamonds / Q of diamonds / J of diamonds / T of diamonds / 9 of diamond
s / 8 of diamonds / 7 of diamonds / 6 of diamonds / 5 of diamonds / 4 of di
amonds / 3 of diamonds / 2 of diamonds / A of clubs / K of clubs / Q of clu
bs / J of clubs / T of clubs / 9 of clubs / 8 of clubs / 7 of clubs / 6 of
clubs / 5 of clubs / 4 of clubs / 3 of clubs / 2 of clubs /
6 of diamonds / 7 of clubs / 9 of hearts / 4 of diamonds / 9 of diamonds /
J of hearts / 3 of spades / 2 of clubs / J of clubs / A of hearts / J of cl
ubs / Q of hearts / Q of diamonds / 7 of clubs / 2 of diamonds / 5 of clubs
/ K of spades / 5 of clubs / 5 of diamonds / 8 of hearts / 3 of clubs / 6
of hearts / A of spades / A of hearts / 4 of hearts / J of diamonds / A of
clubs / 3 of diamonds / A of diamonds / K of diamonds / 6 of clubs / 4 of
spades / 6 of hearts / 7 of spades / K of clubs / 3 of hearts / 3 of hearts
/ K of spades / Q of spades / J of spades / 7 of hearts / J of hearts / K
of hearts / T of spades / K of clubs / 8 of hearts / 2 of spades / A of di
amonds / 4 of diamonds / Q of clubs / T of diamonds / 7 of hearts / 9 of cl
ubs / T of clubs / 7 of spades / 6 of spades / 6 of spades / K of diamonds
/ Q of spades / 7 of diamonds / Q of hearts / J of spades / 3 of clubs / 5
of hearts / K of hearts / 9 of spades / 9 of clubs / 2 of clubs / 7 of dia
monds / 8 of diamonds / T of spades / 8 of clubs / 8 of diamonds / 8 of clu
bs / T of diamonds / 4 of spades / 9 of hearts / A of spades / 8 of spades
/ 4 of hearts / T of hearts / 5 of diamonds / T of hearts / 5 of spades /
9 of diamonds / 5 of hearts / T of clubs / Q of diamonds / 6 of diamonds /
3 of spades / J of diamonds / 8 of spades / Q of clubs / 2 of diamonds / 2
of hearts / 6 of clubs / 2 of hearts / 9 of spades / 4 of clubs / 2 of spad
es / 3 of diamonds / 5 of spades / 4 of clubs / A of clubs /
--------------------------------------------------------- */
--------------------------------------------------------------------------------------
handExplanation / Answer
CardValue is defined in Subsection 5.7.6, and is repeated below just as given there. I've added a toString() method to the definition of Suit from the same section. Given the two enumerated types, we can now represent the suit and the value of a playing card using values of type Suit and CardValue, instead of coding the suit and value as integers. In the Card class, the int variables suit and value should be replaced with variables of type Suit and CardValue. The named constants such as HEARTS and ACE in the Card class, which were used to represent suits and card values, should be deleted; they have been replaced by the constants from the enumerated types. The original Card class included instance methods getSuitAsString() and getValueAsString() to convert the suit and value of a card into an appropriate string representation. These methods are no longer necessary, since each enumerated types has its own toString() method that will automatically be used to do the conversion to string.
Class Card becomes much shorter in this version. (Most of the missing code has been moved into the enumerated types.) We just need the two instance variables to represent the suit and value of a card, getter methods to return the values of these two methods, a toString() method to do the Card-to-String conversion, and a constructor that specifies the suit and value of the card that is being constructed:
I've made just one addition (besides comments) in the version given below. Since enumerated type values are objects, the value of a variable of type Suit or CardValue can be null. However, it doesn't make sense for the suit or value of a Card to be null. As the class stands, there is nothing to stop someone from passing null as the value of one of the actual parameters in the constructor, and that would result in an invalid Card object. To prevent this, I check the parameters in the constructor and throw a NullPointerException if an actual parameter is null.
The main program simply constructs each of the 52 possible cards, and prints out each card. The algorithm uses "for-each" loops to loop through all the possible suits and all the possible card values:
Using the for loop syntax from Subsection 3.4.4, a loop such as "for each Suit" becomes "for (Suit suit : Suit.values())", and this leads to the main() routine shown below.
The Solution
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.