Question
/* * Card.java * * A blueprint class to represent an individual playing card. * * CS 112, Boston University * * completed by: ,
/*
* Card.java
*
* A blueprint class to represent an individual playing card.
*
* CS 112, Boston University
*
* completed by:
*/
public class Card {
// constants for the ranks of non-numeric cards
public static final int ACE = 1;
public static final int JACK = 11;
public static final int QUEEN = 12;
public static final int KING = 13;
// other constants for the ranks
public static final int FIRST_RANK = 1;
public static final int LAST_RANK = 13;
// Arrays of strings for the rank names and abbreviations.
// The name of the rank r is given by RANK_NAMES[r].
// The abbreviation of the rank r is given by RANK_ABBREVS[r].
private static final String[] RANK_NAMES = {
null, "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King"
};
private static final String[] RANK_ABBREVS = {
null, "A", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "J", "Q", "K"
};
// constants for the suits
public static final int FIRST_SUIT = 0;
public static final int LAST_SUIT = 3;
public static final int CLUBS = 0;
public static final int DIAMONDS = 1;
public static final int HEARTS = 2;
public static final int SPADES = 3;
// Arrays of strings for the suit names and abbreviations.
// The name of the suit s is given by SUIT_NAMES[s].
// The abbreviation of the suit s is given by SUIT_ABBREVS[s].
private static final String[] SUIT_NAMES = {
"Clubs", "Diamonds", "Hearts", "Spades"
};
private static final String[] SUIT_ABBREVS = {
"C", "D", "H", "S"
};
/***** part 2: getSuitNum *****/
private static int getSuitNum(String suit) {
// The return statement below is included so the starter code
// will compile.
// Replace it with your implementation of the method.
public static
return 0;
}
/***** Implement parts 3-7 below. *****/
}
2. Implement the getSuitNum method (2 points) Complete the getSuitNum method provided in Card.java. This method takes the name of a suit as a parameter, and it should return the index of the specified suit in the SUIT_NAMES array, or -1 if the string passed as a parameter does not appear in that array. For example: getSuitNum("Hearts") should return 2, because "Hearts" has an index of 2 in the SUIT_NAMES array, and I getSuitNum( "Spades") should return 3, because Spades has an index of 3 in the SUIT_NAMES array. getSuitNum": "foo") should return -1, because foo does not appear in the SUIT_NAMES array. Notes: . Use the equalsIgnoreCase method when testing to see if the parameter matches a given element of the SUIT_NAMES array, so that the method will work for variants of the suit names in which the cases of the letters are different than the cases of the names in the array. For example, getSuitNum( "spades") and getSuitNum("SPADES") should also return 3. This method is a private helper method, and it will be used by one or more of the other methods that you write. . This method (unlike the others you will write) is static, because it does not need to access the fields in the object. Rather, we pass it all of the information that it needs as a parameter. 3. Define the fields (2 points) Each Card object should encapsulate two pieces of state: the card's rank (an integer). For numeric cards, the rank is simply the number itself (e.g., 5 cards have a rank of 5). Aces have a rank of 1, Jacks a rank of 11, Queens a rank of 12, and Kings a rank of 13. Ranks less than 1 or greater than 13 will not be allowed. the card's suit number (an integer). This is the value that would be returned by the getSuitNum method for the card's suit. The only allowable suit numbers are 0, 1, 2, and 3. For example, here is what a Card object representing an Ace of Hearts would look like in memory: +-- 1 rank 1 1 +-----+ | +-----+ | IsuitNum , 2 1 +-----+ 1 -+ Note that it has two fields, both of which are integers. For now, you only need to define the fields. Make sure to: I use the field names shown above 1 protect them from direct access by client code. In subsequent sections, you will write constructors that assign values to the fields, and that ensure that only valid values are allowed
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started