Your assignment is to write and submit two java files. One is a class definition
ID: 3940064 • Letter: Y
Question
Your assignment is to write and submit two java files. One is a class definition named CardPlay (CardPlay.java), and another is the test driver program, Assignment7.java.
1) CardPlay is a class which will generate random cards for a user to be used in any card game, and it has the five instance variables: oneHand, suit, maxCount, currSize and a Random class object rand
CardPlay
oneHand : int[]
suit : int[]
maxCount : int
currSize : int
rand : Random
+ CardPlay(int)
+ generateHand() : void
+ calcSuitFreq() : void
+ getSuitFreq() : int
+ getHand() : String
checkDuplicate(int) : boolean
getSuit(int) : String
getKind(int) : String
1) oneHand is an array object storing randomly generated cards between 0 and 51 as int, 2) suit is an array object storing frequency of suit of user’s cards as int, 3) maxCount is the maximum number of cards, 4) currSize is an int for keeping track of the number of cards generated, and 5) rand is an object of class Random.
The class CardPlay must include the constructors and methods in the table: (If your class does not contain any of the following methods, points will be deducted). The public and private access modifiers are represented as "+" and "-" respectively.
1. CardPlay(int): (4 pts)
The constructor is to
Reserve memory space for the array object oneHand with the length given as parameter variable.
Reserve memory space for the array object suit with the length 4 (i.e. the different kinds of suit available).
The maxCount is set as parameter variable, specifying the maximum number of cards that can be held in this array (length of the array oneHand).
The currSize is set as 0. It is not the length of array but the number of valid cards. In other words, when oneHand is partially filled array, then currSize is used as an END position of valid cards.
Create an instance rand of Random class-type.
2. GenerateHand():void (3 pts)
If there is room (left in) the array oneHand, add a distinct card to the END(currSize) position of the array and adjust the currSize to reflect how many values are in the array oneHand.
Use Random class method(s) to generate an integer value between 0-51. Add this value to array oneHandOnly If the value is distinct, i.e. oneHand array does not contain a similar value.
Q. How to use Random class and it’s methods?
A. Check Lecture Slides 4 (slide number 14 and 15)
3. CheckDuplicate(int):boolean (3 pts)
This method should perform a linear or sequential search. The method is to look at the elements in the array oneHand until the value of the parameter is found or all values have been examined. If a value occurs in the array, the method should return the true. If the value is not in the array, then the method should return false.
4. calcSuitFreq():void (3 pts)
To calculate the suit, we will consider
The first 13 cards are Clubs (From 0 – 12)
The next 13 are Diamonds (From 13 – 25)
After that we have Hearts (From 26 – 38)
Finally, the last 13 cards are Spades (From 39 – 51)
Once the suit is calculated then, update the value for array suit at the appropriate location/index number.
Again for the array suit, we will assume that suit[0] is holding the number of Club cards that user has, suit[1] is holding the number of Diamond cards that user has, suit[2] is holding the number of Heart cards that user has and finally, suit[3] is holding the number of Spade cards that user has.
5. getSuitFreq():int (3 pts)
This method will calculate how many suits have a count of greater than 1. For example, if user cards are following:
Jack of Club
2 of Diamond
5 of Spade
King of Diamond
Ten of Club
Then this method will calculate that user has Clubs with a count of greater than 1 and Diamonds with a count of greater than 1. Therefore, it will return a value 2.
6. getHand():String (3 pts)
Return the cards in the array as shown below.
Jack of Club
2 of Diamond
5 of Spade
King of Diamond
Ten of Club
7. getSuit(int):String
Find the suit of the card. An almost code is given to you below. You can choose to use this code or write your own. Make sure to type it in your java file, DO NOT copy directly from this PDF document or you might get special symbols copied which will generate errors on the Submission website.
/**
There is a one to one correspondence of the integers 0 through 51 and each playing card.
@param cardNum is in the range 0 - 51
@return suit of the corresponding card
*/
private String getSuit(int cardNum) {
// The first 13 cards are Clubs
if (cardNum< 13)
return "Club";
// The next 13 are Diamonds else if (cardNum< 26)
return "Diamond";
// Then Hearts... else if (cardNum< 39) return "Heart";
// Or Spades. else
return "Spade";
}
8. getKind(int):String
Find the Kind of the card. An almost code is given to you below. You can choose to use this code or write your own. Make sure to type it in your java file, DO NOT copy directly from this PDF document or you might get special symbols copied which will generate errors on the Submission website.
/**
There is a one to one correspondence of the integers 0 through 51 and each playing card.
@param cardNum is in the range 0 - 51
@return kind of the corresponding card
*/
private String getKind(int cardNum) {
// We'll assign these four cards as Kings.
if (cardNum%13 == 0) return "King";
// These as Aces. else if (cardNum%13 == 1) return "Ace";
// These as Tens else if (cardNum%13 == 10)
return "Ten";
// Jacks
else if (cardNum%13 == 11)
return "Jack";
// Queens
else if (cardNum%13 == 12) return "Queen";
// This naturally takes care of 2 through 9.
else
return Integer.toString(cardNum%13);
}
Important Notes: Your class CardPlay should have exactly the method headers that are described or otherwise your class will not work with the test driver program (Assignment7.java) that is provided.
You should never change the test driver program, i.e., Assignment7.java, but instead make changes to CardPlay class to make it work.
Output Example
Program Output (Input in bold)
Welcome to CardPlay Club ----------------------------------- n: Generate a hand of cards p: Print information of user's hand s: Calculate suit Frequency f: Print suit Frequency ?: Display the menu again q: Quit this program
Please enter a command or type ?: n Creating a new hand of cards for the user...
Created!
Please enter a command or type ?: p
User's cards are:
Ace of Spade
2 of Spade
7 of Club
Jack of Spade
Jack of Diamond
Please enter a command or type ?: s Calculating suit frequency for user's cards...
Calculated!
Please enter a command or type ?: f
User has 1 suit with more than one card
Please enter a command or type ?: q
CardPlay
oneHand : int[]
suit : int[]
maxCount : int
currSize : int
rand : Random
+ CardPlay(int)
+ generateHand() : void
+ calcSuitFreq() : void
+ getSuitFreq() : int
+ getHand() : String
checkDuplicate(int) : boolean
getSuit(int) : String
getKind(int) : String