public class Card { // Constant. public final static int MAX_RANK = 13; // Enumerated type to satisfy s

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

public class Card { // Constant. public final static int MAX_RANK = 13; // Enumerated type to satisfy s

Post by answerhappygod »

public class Card
{
// Constant.
public final static int MAX_RANK = 13;

// Enumerated type to satisfy second class
invariant.
public enum SuitType {CLUBS, HEARTS, SPADES,
DIAMONDS}
// Attributes.
private SuitType suit;
private int rank;

/**
* Constructor for objects of class Card
*/
public Card()
{
// initialize rank and suit
this.rank = 1;
this.suit = SuitType.HEARTS;
}
/**
* Constructor for objects of class Card
*/
public Card(int theRank, SuitType theSuit)
{
// initialize rank and suit
this.rank = theRank;
this.suit = theSuit;
}

/**
* getSuit:
*
* @return the suit
*/
public SuitType getSuit()
{
// put your code here
return suit;
}

/**
* setSuit:
*
* @param the suit
*/
public void setSuit(SuitType theSuit)
{
this.suit = theSuit;
}

/**
* getRank:
*
* @return the rank
*/
public int getRank()
{
// put your code here
return rank;
}

/**
* setRank: set the rank attribute to the value
of the parameter if
* the parameter is between 1 and MAX_RANK
(inclusive) to satisfy the first class invariant.
*
* @param the rank
*/
public void setRank(int theRank)
{
if (theRank >= 1 && theRank
<= MAX_RANK)
this.rank =
theRank;
}

/**
* toString:
*
* @return a String representation of a Card
that includes all attributes, i.e. rank and suit.
*/
public String toString()
{
String result = "";

if (this.rank == 1)
result += "ACE";
else if (this.rank == 11)
result += "JACK";
else if (this.rank == 12)
result += "QUEEN";
else if (this.rank == 13)
result += "KING";
else
result +=
String.valueOf(this.rank); // Convert int to String

result += " of " +
this.suit.name();

return result;
}

}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply