Your assignment is to write and submit two java files One is
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 |
Solution
/** * An object of type Card represents a playing card from a * standard Poker deck, including Jokers. The card has a suit, which * can be spades, hearts, diamonds, clubs, or joker. A spade, heart, * diamond, or club has one of the 13 values: ace, 2, 3, 4, 5, 6, 7, * 8, 9, 10, jack, queen, or king. Note that \"ace\" is considered to be * the smallest value. A joker can also have an associated value; * this value can be anything and can be used to keep track of several * different jokers. */ public class Card { public final static int SPADES = 0; // Codes for the 4 suits, plus Joker. public final static int HEARTS = 1; public final static int DIAMONDS = 2; public final static int CLUBS = 3; public final static int JOKER = 4; public final static int ACE = 1; // Codes for the non-numeric cards. public final static int JACK = 11; // Cards 2 through 10 have their public final static int QUEEN = 12; // numerical values for their codes. public final static int KING = 13; /** * This card\'s suit, one of the constants SPADES, HEARTS, DIAMONDS, * CLUBS, or JOKER. The suit cannot be changed after the card is * constructed. */ private final int suit; /** * The card\'s value. For a normal cards, this is one of the values * 1 through 13, with 1 representing ACE. For a JOKER, the value * can be anything. The value cannot be changed after the card * is constructed. */ private final int value; /** * Creates a Joker, with 1 as the associated value. (Note that * \"new Card()\" is equivalent to \"new Card(1,Card.JOKER)\".) */ public Card() { suit = JOKER; value = 1; } /** * Creates a card with a specified suit and value. * @param theValue the value of the new card. For a regular card (non-joker), * the value must be in the range 1 through 13, with 1 representing an Ace. * You can use the constants Card.ACE, Card.JACK, Card.QUEEN, and Card.KING. * For a Joker, the value can be anything. * @param theSuit the suit of the new card. This must be one of the values * Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER. * @throws IllegalArgumentException if the parameter values are not in the * permissible ranges */ public Card(int theValue, int theSuit) { if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS && theSuit != CLUBS && theSuit != JOKER) throw new IllegalArgumentException(\"Illegal playing card suit\"); if (theSuit != JOKER && (theValue < 1 || theValue > 13)) throw new IllegalArgumentException(\"Illegal playing card value\"); value = theValue; suit = theSuit; } /** * Returns the suit of this card. * @returns the suit, which is one of the constants Card.SPADES, * Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER */ public int getSuit() { return suit; } /** * Returns the value of this card. * @return the value, which is one the numbers 1 through 13, inclusive for * a regular card, and which can be any value for a Joker. */ public int getValue() { return value; } /** * Returns a String representation of the card\'s suit. * @return one of the strings \"Spades\", \"Hearts\", \"Diamonds\", \"Clubs\" * or \"Joker\". */ public String getSuitAsString() { switch ( suit ) { case SPADES: return \"Spades\"; case HEARTS: return \"Hearts\"; case DIAMONDS: return \"Diamonds\"; case CLUBS: return \"Clubs\"; default: return \"Joker\"; } } /** * Returns a String representation of the card\'s value. * @return for a regular card, one of the strings \"Ace\", \"2\", * \"3\", ..., \"10\", \"Jack\", \"Queen\", or \"King\". For a Joker, the * string is always a numerical. */ public String getValueAsString() { if (suit == JOKER) return \"\" + value; else { switch ( value ) { case 1: return \"Ace\"; case 2: return \"2\"; case 3: return \"3\"; case 4: return \"4\"; case 5: return \"5\"; case 6: return \"6\"; case 7: return \"7\"; case 8: return \"8\"; case 9: return \"9\"; case 10: return \"10\"; case 11: return \"Jack\"; case 12: return \"Queen\"; default: return \"King\"; } } } /** * Returns a string representation of this card, including both * its suit and its value (except that for a Joker with value 1, * the return value is just \"Joker\"). Sample return values * are: \"Queen of Hearts\", \"10 of Diamonds\", \"Ace of Spades\", * \"Joker\", \"Joker #2\" */ public String toString() { if (suit == JOKER) { if (value == 1) return \"Joker\"; else return \"Joker #\" + value; } else return getValueAsString() + \" of \" + getSuitAsString(); } } // end class Card




