Question
Getting started Download the following file: Card.java Make sure to put it in your ps2 folder. As needed, open your ps2 folder using the File->Open
Getting started
-
Download the following file: Card.java Make sure to put it in your ps2 folder.
-
As needed, open your ps2 folder using the File->Open Folder or File->Open menu option in VS Code. The name of the folder should appear in the Explorer pane on the left-hand side of the VS Code window, along with the name of the Card.java file that you just downloaded.
-
Click on the name Card.java, which will open an editor window for that file. You will see some starter code that weve provided.
card.java
/*
* 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.
return 0;
}
/***** Implement parts 3-7 below. *****/
}
Your tasks
-
(0 points) Start by reading over the starter code that weve given you including all of the comments that we have provided. In particular, make sure that you understand the following class constants:
-
integer constants for the ranks of non-numeric cards (ACE, JACK, QUEEN, and KING)
-
arrays of strings representing the names and abbreviations of the ranks. These arrays are defined so that the rank number can be used as the index into the array: the numeric rank r has the name RANK_NAMES[r] and the abbreviation RANK_ABBREVS[r]. For example, an Ace has a rank of 1, and thus RANK_NAMES[1] is the string Ace and RANK_ABBREVS[1] is the string A.
-
integer constants for the four suits (DIAMONDS, HEARTS, CLUBS, and SPADES)
-
arrays of strings representing the names and abbreviations of the suits. These arrays are defined so that the suit number can be used as the index into the array: the numeric suit s has the name SUIT_NAMES[s] and the abbreviation SUIT_ABBREVS[s]. For example, the suit number for DIAMONDS is 1, and thus SUIT_NAMES[1] is the string Diamonds and SUIT_ABBREVS[1] is the string D.
-
-
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
-
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") andgetSuitNum("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.
-
-
Define the fields (2 points) Each Card object should encapsulate two pieces of state:
-
the cards 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 cards suit number (an integer). This is the value that would be returned by the getSuitNum method for the cards 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:
Note that it has two fields, both of which are integers.
For now, you only need to define the fields. Make sure to:
-
use the field names shown above
-
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.
-
-
Implement the constructors (3 points)
In Java, a class can have more than one constructor; this provides clients of the class with different options for creating a new object of the class. All of the constructors must be named after the class, but their parameter lists must differ from each other in some way.
You should add the following two constructors to the Card class:
-
a constructor that takes two integer parameters specifying the cards rank and suit number (in that order). It should ensure that only valid values are assigned to the objects fields, as specified in part 3.
-
a constructor that takes an integer parameter specifying the cards rank and a String parameter specifying the cards suit (in that order). Note that this constructor will need to determine the suit number for the specified suit string, and it should use another method that you have already written for this purpose. Here again, the constructor should ensure that only valid values are assigned to the objects fields.
For example, here is some code that uses the constructors:
-
-
Implement the basic accessor methods (7 points) Next, define the following initial set of instance methods. (Note that all of these methods are accessor methods. We will not implement any mutator methods, because we assume that a given Card objects rank and suit number remain fixed once the object is created. The constructor will take care of assigning the initial values of those fields, and those values will not change.)
-
getRank, which returns the integer representing the Card objects rank. For example, if c1 is the card shown in the diagram above, c1.getRank() should return 1.
-
getRankName, which returns a String representation of the Card objects rank. For example, if c1 is the card shown in the diagram above, c1.getRankName() should return the string "Ace". This method should make use of the array of rank names that we have given you.
-
getSuitNum, which returns the Card objects suit number. For example, if c1 is the card shown in the diagram above, c1.getSuitNum() should return 2. (Note that this method is different from the method that you wrote for part 2. That method is a private static helper method; because it is static, it doesnt have a called object. The method that you are writing here is non-static, and it should return the suit number of the called object.)
-
getSuitName, which returns a String representation of the Card objects suit. For example, if c1 is the card shown in the diagram above, c1.getSuitName() should return the string "Hearts". This method should make use of the array of suit names that we have given you.
-
getName, which returns a String representing the full name of the Card. The returned String should have the form rank_name of suit_name. For example, if a Cardobject represents a 10 of Diamonds, this method should return "10 of Diamonds". If a Card object represents a Queen of Spades, this method should return "Queen of Spades". This method can either make use of the RANK_NAMES and SUIT_NAMESarrays that we have given you, or it can use other accessor methods that you have already written.
-
isAce, which returns true if the Card is an Ace and and false if it is not.
-
isFaceCard, which returns true if the Card is a face card (Jack, Queen, or King) and and false if it is not.
-
getValue, which returns the Card objects value. If the card is a face card, then it should return a value of 10. Otherwise, it should return the cards rank. Hint: Use the isFaceCard() method to check if it is a face card!
Make sure that your methods are non-static, because they need access to the fields in the called object. In addition, none of these methods should take an explicit parameter, because all of the information that they need is inside the called object.
Once you have completed your methods for parts 4 and 5, you can test them using the first client program that weve given you. See below for more detail.
-
-
Define the toString method (2 points) Write a toString method that returns a String representation of the Card object that can be used when printing it or concatenating it to a String. We will discuss this type of method in lecture, and we provide an example in our Rectangle class. The returned String should consist of the cards rank abbreviation followed immediately by its suit abbreviation. For example, if a Card object represents a 10 of Diamonds, this method should return "10D". If a Card object represents a Queen of Spades, this method should return "QS". This method must make use of the RANK_ABBREVS and SUIT_ABBREVS arrays that we have given you.
-
Define methods for comparing Card objects (3 points) Finally, define the following two instance methods for comparing Card objects:
-
sameSuitAs, which takes a Card object as a parameter and determines if it is has the same suit as the called object, returning true if they have the same suit and false if they do not have the same suit. If a value of null is passed in for the parameter, the method should return false.
-
equals, which takes a Card object as a parameter and determines if it is equivalent to the called object, returning true if it is equivalent and false if it is not equivalent. Two Card objects should be considered equivalent if their rank and suit are the same. If null is passed in for the parameter, the method should return false.
-
In addition to using the client programs, we recommend that you perform additional testing on your own. You can do so by adding code to one of the clients, or by adding a mainmethod to the Card class.
Note: The static getSuitNum() method from part 2 is not directly tested by the clients. However, it should be tested indirectly by Client1.java, because it should be called by one or more of the other methods that you write. If you wanted to test it directly, you could either add a main method to your Card class and put some test code for it in that method, or you could temporarily change it from private to public so that you can test it from the clients. (Private methods cannot be tested from the clients, because they can only be called from inside the class.)
-+ - 1 -+ | +-----+ | rankl 1 | +-----+ | | + 1 I suitNum 2 | 1 +-----+ | -+ 2 +- -----+
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