CS 150, Fall 2017
Programming Assignment - P7
Dealing Cards Using Arrays and For Loops
Due - October 31th, 2017 at 6:00pm
Late - November 1st, 2017 at 8:00am
Objectives of this Assignment
- Write a program that deals one hand of 20 cards using arrays and for loops, and
- do so by using the Random class to generate pseudorandom values between 0 and 51, and
- initialize the player hand with 20 integer values, but also
- keep track if a value 0 through 51 has already been dealt, and lastly
- determine what the suits and ranks of cards are in the player hand based on the integer values.
Create a Java program called P7 with a class named P7, which after created should contain a file named P7.java in the src folder. All of the Java code you write should be contained in the main method. This assignment is based on dealing one hand of 20 cards from a standard 52-card deck. However, because we are using arrays, each card in the deck will be represented by the integers 0-51.
The setup for which integers belong to a certain card suit and the respective ranks per suit are based upon the design described by the Liang textbook and referenced in this class in the Chapter 7 slide set, starting on slide 41. It is recommended you understand how to determine a certain card from a given integer value before beginning this assignment. Doing so presents another opportunity to review the difference between the division and modulus operators when performing integer math. For this assignment, you must follow the directions for output formatting below exactly:
- Declare and initialize a boolean array with space to hold 52 elements - this array will play the role of determining whether or not a card has already been dealt.
- Declare and initialize an integer array with space to hold 20 elements - this array will, after program execution, be assigned 20 random integer values (0-51) that you will then use to determine which specific cards of the deck a player has.
- Next, you will need to declare and initialize three String arrays.
- The first String array will have the four possible suits in a deck of cards. Initialize this array to
"S", "H", "D", "C" (in that order).
- The second String array will have the 13 possible ranks in a deck of cards. Initialize this array to
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" (in that order).
- The third String array should be initialized to hold 20 elements and each element will, after program execution, be assigned a String that represents one card of a standard deck. For example: "C6" is the six of clubs; "H3" is the three of hearts; "SQ" is the queen of spades; and "DA" would be the ace of diamonds.
- Last, declare and initialize two integer variables. One will be the seed value for the random number generator. Initialize this variable to
Integer.parseInt(args). The other variable will represent the card to be dealt to the player. Initialize this to -1.
- Write a for loop that assigns false to every element in the array of boolean values.
- Next, instantiate the Random class and set the seed of the Random class object using the following code:
Random r = new Random();
NOTE: Do not forget you will need to import the Random class library at top of your P7.java file in order to be able to use methods of the Random class.
- For the first part of this program, you will write a for loop with a do-while loop inside in order to assign the player hand with 20 different integer values.
- First, write a for loop that iterates over your 20-element integer array (remember this array will have the cards in the player hand represented as integers).
- Inside this loop, insert a do-while loop with the following code inside:
cardDealtToPlayerVariable = r.nextInt(52);
NOTE: The nextInt() method of the Random class "returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence".
- Your do-while loop must continue to iterate as long as the random number generated has already been dealt to the player. In this program, we are keeping track of already-dealt card values in the boolean array. So, since the nextInt() method generates a value 0-51, we can also use this as the index value to check the boolean array.
For the boolean expression of the while, this time you will not enter an expression to be evaluated, such as 5 <= 3, but instead access the boolean array at the integer value generated inside the do-while loop. If the element at that location is set to true, the do-while loop will iterate again and generate a new integer value.
- Next, while still inside the for loop, but outside of the do-while loop, assign the boolean array at the index location specified by the random integer value to true.
- Then also assign the respective element of the player hand array the same random integer value.
- The next for loop will assign an appropriate card value in its String form to each element of your third String array that currently has 20 null elements.
NOTE: When you initialize a String array to be a certain size, but do not assign each element a certain value, Java automatically assigns each element the default value of null.
- In this for loop you will need to access each integer currently in the player hand integer array. Then to determine which card suit the integer value relates to, you will divide by 13. The resulting value will then correspond to one of the four suits in the card suit array.
Next, to determine which card rank the integer value relates to, you will modulo by 13. The resulting value will then correspond to one of the thirteen ranks in the card rank array.
- Using the appropriate single-character Strings from the suit and rank arrays, create a two-character String by concatenating the values and assign the player hand String array a new value. The new String value should be assigned to the same index location in the String array as in the player hand integer array.
- Steps 15, 16, and 17 can be executed in one line of code. However, you will likely be able to keep track of what is going on if you break up each step into several lines of code. In this case you will need to create a few additional program variables to use in this for loop.
- Now it is time to print out the resulting player hand.
- First, print the following prompt on its own line:
- After this prompt, write a for loop to iterate over the String array now holding the cards of the player hand.
- In the for loop, print the array elements to the console followed by a comma and single space. DO NOT include a comma after the last element is printed. In effect, how should you change the upper bound of your for loop?
- For grading purposes, the hand will be printed ten cards to a line. Using a conditional in the for loop, print a blank line to the console after printing the tenth element of the array. This conditional must only execute printing a blank line once, so think of the appropriate type of boolean expression to make that happen.
- Lastly, outside of the for loop you will need to write one more line of code that will allow array elements to be displayed as C6, D7, H8, ... DK, HQ instead of C6, D7, H8, ... DK, HQ,. Think about what happened by altering the upper bound of the for loop above. Remember that your last card must have an end of line character after it as well.
Testing Your Code
- When you are ready to test your code you will need to set up one of the options under the Run Configurations for your project.
- To do so, go to the Run menu and select Run Configurations...
- In the Run Configurations pane click the (x)= Arguments tab underneath Name: P7.
- In the Program Arguments box, enter a value 0-9. Then click Apply and then Run.
- NOTE: Do not make any other modifications to your run configurations at this time, as they may cause undesired results when your program executes.
- NOTE: The integer program argument you place into Program Arguments is used in your program when the following line of code executes:
Integer.parseInt(args). args specifically refers to the zeroth position of the command line arguments and, in Eclipse, tells a program to use whatever value is listed first in the Program Arguments box. For those of you who plan to continue into CS163/165, you will use this type of setup again several times in these classes. One benefit of this setup is that we no longer need to prompt the user for a value, but instead set the value in the background, and simply tell the program where to go to find the value we want to use.
Your program should print 3 lines. Shown below is the output for seed values 0-3. Testing will use different seed values.
Seed Value = 0
C6, CJ, C9, CA, S10, S8, C3, S4, C4, D4,
H9, S3, C5, SA, H7, HQ, D2, D3, C10, SK
Seed Value = 1
H5, SA, S8, S10, DK, HQ, C8, H10, DJ, CJ,
HA, D6, C4, D7, D5, S5, S2, C2, DA, H3
Seed Value = 2
C6, D7, SA, C9, H5, D5, D9, CK, CA, S5,
S3, CQ, SJ, DA, C10, H3, C5, H10, D2, SQ
Seed Value = 3
H6, SK, DK, D8, H4, C8, H9, D7, D10, CJ,
C3, CQ, S2, S3, HK, S5, H7, CK, C2, D2
- Work on your own, as always.
- The name of the source code file must be exactly
- Name the file exactly - upper and lower case matters!
- Comments at the top as shown above.
- Assignments should be implemented using Eclipse.
- Assignments should be implemented using Java, version 1.8.
- Make sure your code runs on machines in the CSB 120 lab.
- Turn in through the Checkin tab.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Preliminary Tests
Preliminary testing tests lines 1-5 .
- testCompile: checks that program compiles. (5 points)
- testMenu: verify correct menu printed (15 points)
- testOptions: 10 points each (5)
- Final Tests
Final testing tests
- testOptions: 10 points each (3)
- Final grading includes the preliminary tests.
Submit P7.java to Checkin.