Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Obiectives: Java Arrays (chapter 7) Write a program named Pokersetup.java that t

ID: 3705678 • Letter: O

Question

Obiectives: Java Arrays (chapter 7) Write a program named Pokersetup.java that takes a command-line argument N and prints N poker hands (five cards each) from a shuffled deck, separated by blank lines. Notel: Instead of getting N from the command-line argument args [0], you may use Scanner or optionPane.showInputDialog) You may assume that N will be between 2 and 8, so there will always be enough cards to deal. Note 2: You must first create a shuffled deck of cards. Note3: use the code on slide 30 of-07 Arrays and ArrayLists" Here is a sample run corresponding to the command % java Pokersetup 3 Player 1 4 of Spades 9 of Spades Ace of Hearts 9 of Clubs 9 of Diamonds Player 2 6 of Spades 10 of Hearts Queen of Hearts 8 of Hearts King of Spades Player 3 7 of Hearts 8 of Diamonds Queen of Spades 3 of Spades 4 of Diamonds Hint for simplicity, assume that all the five cards are dealt to player 1, then all the five cards to player 2, then all the five cards to player 3, etc. instead of first card to each player, then second card to each player, then third card to each player, etc. This simplifies the coding, because it implies that you do not have to store each hand in a separate array. All you need is one array for the entire card deck, then you print from the beginning of the array, one hand at a time..

Explanation / Answer

Deck.java

==================

import java.util.Scanner;

public class Deck {

public static void main(String[] args) {

String suit[] = {"Clubs","Diamonds","Hearts","Spades"};

String rank[] = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};

int SUITS = suit.length;

int RANKS = rank.length;

int N = SUITS*RANKS;

//build the deck

String [] deck = new String[N];

for(int i=0;i<RANKS;i++)

for(int j=0;j<SUITS;j++)

deck[SUITS*i + j] = rank[i] +" of "+suit[j];

//suffle

for(int i=0;i<N;i++){

int r = i + (int)(Math.random()*(N - i));

String t = deck[r];

deck[r] = deck[i];

deck[i] = t;

}

//print suffel deck

/*for(int i=0;i<N;i++)

System.out.println(deck[i]);*/

//Newly added codes

Scanner sc=new Scanner(System.in); //scanner class used

System.out.println("Enter no. of plyers :");

int number = sc.nextInt();

while (!(number>= 2 && number<=8))

{

System.out.println("No. of players should be within 2 to 8. Please re-enter :");

number = sc.nextInt();

}

//printing the cards for each player

for(int i=0;i<number;i++)

{

System.out.println(" Player "+(i+1)+":");

for(int j=0;j<5;j++)

{

System.out.println(deck[5*i + j]);

}

}

}

}