This page is part of the Blackjack Lab.
A BlackjackCard
represents a playing card used for Blackjack. Responsibilities include returning the numeric value according to Blackjack rules and returning a string representation.
Start files:
Solutions are at the bottom of this page.
Provided code
Constants
Constants are provided for the values of several specific cards (ex: JACK_VALUE
). The constant FACE_CARD_NUMERICAL_VALUE
is also provided. These constants are used throughout both the documentation and the code.
Magic numbers are numeric literals that appear within code (ex: 10
). Using constants instead of magic numbers makes code easier to read and maintain.
For example, the first conditional statement in the constructor could be written as:
if(! (1 <= value && value <= 13) )
Instead, the statement is written as:
if(! (ACE_VALUE <= value && value <= KING_VALUE) )
Using the constants makes the intent clearer.
The public
constants (ex: ACE_VALUE
) are intended for use both inside and outside BlackjackCard
. The private
constant FACE_CARD_NUMERICAL_VALUE
is intended for use only within BlackjackCard
.
Instance variables
BlackjackCard
stores value
and suit
. The values are set directly to the constructor parameters. The documentation for the constructor describes the valid values.
No other instance variables should be added.
BlackjackCard
constructor
The constructor accepts only value valid values for value
and suit
. Passing invalid values results in an IllegalArgumentException
. This behavior makes errors in client code (code that uses BlackjackCard
) easier to identify.
getValue
and getSuit
methods
getValue
and getSuit
are simple accessor methods. The instance variables value
and suit
are provide. The accessor methods provide the values of these variables to client code.
A BlackjackCard
is immutable. Neither the value nor the suit can be changed after the card is constructed. There is no setValue
method.
Code to be written
isAce
method
Aces are handled differently than other cards. The isAce
allows client code to be written more cleanly. The isAce
method is not strictly required.
Client code could write:
if(card.getValue() == BlackjackCard.ACE_VALUE)
The isAce
method allows the code to be more cleanly written as:
if(card.isAce())
Many classes contain methods that exist to allow cleaner client code. The java.util.ArrayList
class provides both add(int, E)
and add(E)
. The 2 parameter add
method can be used to add a value to the end of a list; however, the single parameter method allows that code to be written more cleanly.
getNumericalValue
method
In Blackjack, the numerical value of numbered cards (2 - 10) is the number. All face cards are worth 10
(FACE_CARD_NUMERICAL_VALUE
). getNumericalValue
returns 1
(ACE_VALUE
) for an ace.
getNumericalValue
is an accessor method. It should not change the value of the instance variable value
.
toString
method
toString
is responsible for returning a string representation. Examples are provided in the Javadoc.
toString
is an accessor method. It should not change the values of either instance variable (value
or suit
).
Hints
isAce
method
Compare the value of the instance variable value
against the constant ACE_VALUE
.
getNumericalValue
method
Handle the special cards first. Face cards (jack, queen, and king) and aces are special.
The value of numbered cards (2 - 10) is value
.
toString
method
Handle each special card first. The special cards are the same as in getNumericalValue
, though each must be handled separately, unlikely in getNumericalValue
.
All of the numbered cards can be handled with a single statement.
Solution code
isAce
method
public boolean isAce()
{
return value == ACE_VALUE;
}
The instance variable value
already stores the information necessary to determine if this BlackjackCard
is an ace. No additional instance variable should be stored. See Duplicate data as instance variables.
getNumericalValue
method
public int getNumericalValue()
{
if(value >= JACK_VALUE)
return FACE_CARD_NUMERICAL_VALUE;
else
return value;
}
As with isAce
, the existing instance variable value
stores all necessary information. value
should not be modified, nor should an additional instance variable be stored.
toString
method
public String toString()
{
String asString = "";
if(value == ACE_VALUE)
asString += "A";
else if(value == JACK_VALUE)
asString += "J";
else if(value == QUEEN_VALUE)
asString += "Q";
else if(value == KING_VALUE)
asString += "K";
else
asString += value;
asString += suit;
return asString;
}