# CS 150, Fall 2017

## Dealing Cards Using Arrays and For Loops

#### Objectives of this Assignment

1. Write a program that deals one hand of 20 cards using arrays and for loops, and
2. do so by using the Random class to generate pseudorandom values between 0 and 51, and
3. initialize the player hand with 20 integer values, but also
4. keep track if a value 0 through 51 has already been dealt, and lastly
5. determine what the suits and ranks of cards are in the player hand based on the integer values.

#### Instructions

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:

Part One
1. 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.
2. 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.
3. 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.
4. 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[0])`. The other variable will represent the card to be dealt to the player. Initialize this to -1.

5. Part Two
6. Write a for loop that assigns false to every element in the array of boolean values.
7. Next, instantiate the Random class and set the seed of the Random class object using the following code:
```  Random r = new Random();
r.setSeed(yourSeedValueVariable); ```
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.
8. 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.
9. 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).
10. Inside this loop, insert a do-while loop with the following code inside:
11. ` 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".
12. 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.
13. 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.
14. Then also assign the respective element of the player hand array the same random integer value.

15. Part Three
16. 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.
17. 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.
18. 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.
19. 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.

20. Part Four
21. Now it is time to print out the resulting player hand.
22. First, print the following prompt on its own line:
23. ` Player hand: `
24. After this prompt, write a for loop to iterate over the String array now holding the cards of the player hand.
25. 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?
26. 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.
27. 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.

28. Testing Your Code
29. 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.
30. To do so, go to the Run menu and select Run Configurations...
31. In the Run Configurations pane click the (x)= Arguments tab underneath Name: P7.
32. In the Program Arguments box, enter a value 0-9. Then click Apply and then Run.
33. NOTE: Do not make any other modifications to your run configurations at this time, as they may cause undesired results when your program executes.
34. 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[0])`. args[0] 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.

#### Sample Output

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
```Player hand:
C6, CJ, C9, CA, S10, S8, C3, S4, C4, D4,
H9, S3, C5, SA, H7, HQ, D2, D3, C10, SK

```
Seed Value = 1
```Player hand:
H5, SA, S8, S10, DK, HQ, C8, H10, DJ, CJ,
HA, D6, C4, D7, D5, S5, S2, C2, DA, H3

```
Seed Value = 2
```Player hand:
C6, D7, SA, C9, H5, D5, D9, CK, CA, S5,
S3, CQ, SJ, DA, C10, H3, C5, H10, D2, SQ

```
Seed Value = 3
```Player hand:
H6, SK, DK, D8, H4, C8, H9, D7, D10, CJ,
C3, CQ, S2, S3, HK, S5, H7, CK, C2, D2
```

#### Specifications

• Work on your own, as always.
• The name of the source code file must be exactly
` P7.java `
• 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.