Course: Introduction to Computers & Programming (JAVA), Summer 2009
Instructor: Christopher Conway

# Homework #5

## Due: Thursday, 18 June 2009 at 11:59 PM

In this assignment, you will use a random number generator, loops, and methods that you define to simulate a poker hand between four players.

Play proceeds as follows: four players are dealt 5 cards each from a standard 52-card deck. The cards are dealt around the table to each player in turn, with Player #1 getting the 1st card, Player #2 getting the 2nd card, Player #3 getting the 3rd card, Player #4 getting the 4th card, Player #1 getting the 5th card, and so on, until a total of 20 cards are dealt.

Example run:

```Player 1        Player 2        Player 3        Player 4
5 of Diamonds   A of Clubs      K of Spades    10 of Diamonds
10 of Spades     9 of Diamonds   J of Diamonds   7 of Diamonds
J of Clubs     10 of Clubs      7 of Spades     J of Clubs
Q of Hearts     3 of Diamonds   A of Hearts     3 of Hearts
9 of Spades     K of Diamonds   4 of Spades     K of Hearts
```

REQUIREMENTS:

• Use Math.random() to generate a random number between 1 and 13 (1 is an Ace, 11 is a Jack, 12 is a Queen, and 13 is a King ... 2 thru 10 obviously represent the card values 2 thru 10) each time you deal a card.

• Use Math.random() to generate a random number between 1 and 4 (1 is Diamonds, 2 is Hearts, 3 is Spades, and 4 is Clubs) each time you deal a card
• Define the number of players and the `int` values representing face cards and suits as constants. Do not use "magic numbers" in your code.
• You MUST include 2 methods (1 to handle the printing of the card value, and 1 to handle the printing of the card suit) Each must contain a switch statement. Make sure your program does not print something like 12 of Spades (this is why you need the switch statements).

For example, you should have one method with the following header:

public static void printSuit (int suit)

which would print the suit of the card.

• You MUST have 2 for loops, one nested within the other. The outer for loop handles the round being dealt (Rounds 1 thru 5) while the inner loop handles the player being dealt (Players 1 thru 4). Using these repetition structures in this manner will allow you to deal cards in the order described above. DO NOT HAVE YOUR PROGRAM DEAL 5 CONSECUTIVE CARDS TO PLAYER #1, THEN 5 CONSECUTIVE CARDS TO PLAYER #2, ETC. ... THIS IS NOT ACCEPTABLE.

• You may need to use if/else statements to properly print the results in a table as displayed in the above example. You will make use of the "\t" escape sequence (to advance the cursor to next horizontal tab position) and the "\n" escape sequence (to move the cursor to the beginning of the next line).

• THE OUTPUT OF YOUR PROGRAM MUST EXACTLY MATCH THE FORMAT IN THE EXAMPLE ABOVE!

• Note that your program may actually "deal" the same card more than once (for example, the Jack of Clubs appears twice in the example above). This is OK.

### Extra Credit (5 points):

Design your program so that it calls Math.random only once for each card. If you choose to do it this way, use Math.random() to generate a number from 0 to 51. Then use the division and modulus operators to map each integer in that range to a card in the deck. You MUST make sure each of the 52 numbers in that range correspond to a unique card.

Style counts:

• You should use comments throughout the source code to explain key steps and calculations
• Choose intuitive names for variables
• You should use proper indentation (2-4 spaces) and blank lines to make your program easy to read.
• Do not mix tab and space characters in your indentation—configure your editor to replace tabs with spaces. (Instructions here.)
• Do not exceed 80 characters per line.

Your assignment should be submitted BY EMAIL to our class grader: Andrew Lott, adl316 (AT) nyu (DOT) edu.

• Name your .java file `Lastname5.java`. For example: `Conway5.java`.
• Attach your .java file—and only your .java file—to an email with the subject line "Homework #5".
• Each .java file you submit should begin with a comment of the form:
```/**********************************
* [NAME OF YOUR PROGRAM AND A SHORT DESCRIPTION OF ITS PURPOSE]
* Date: [MONTH DAY, YEAR]
* NYU ID: [YOUR NYU NETID]
**********************************/```
Any submission without such a comment will lose 1 point for style.