public static void main(String[] args){ int[] deck = new int[52]; //Initialize d
ID: 3596891 • Letter: P
Question
public static void main(String[] args){
int[] deck = new int[52];
//Initialize deck to cards which range from 8 to 59 inclusively
Use a for loop to initialize the deck...deck[0]=8, deck[1]=9,
...deck[51]=59.
//End of initialize block
System.out.println("Initialized deck");
printDeck(deck);
//Display the sum of card values between index1 and index2
// inclusively
Then use a for loop,
going from index1 to index2. In that loop sum up the card
values found between [index1..index2]
int index1 = 1, index2 = 10, sum = 0;
Your code here
//End of display block
System.out.println("Sum of card values between indexes " +
index1 + " and " + index2 + " is " + sum + " ");
//Shuffle deck of cards
Shuffle the cards using the approach shown in the video
//End of shuffle
System.out.println("Shuffled deck");
printDeck(deck);
//Use selection sort to order the cards in deck
Sort the deck of cards using the code shown in the video
//End of selection sort
System.out.println("Sorted deck");
printDeck(deck);
}
public static void printDeck(int[] deck){
for (int c = 0; c < 52; c++){
System.out.printf("%3d", deck[c]);
if (c % 13 == 12) System.out.println();
}
System.out.println();
}
}
Below is a sample run
Initialized deck
8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46
47 48 49 50 51 52 53 54 55 56 57 58 59
Sum of card values between indexes 1 and 10 is 135
Shuffled deck
42 9 54 37 24 16 17 27 36 44 25 53 46
11 30 48 28 21 52 18 35 56 32 41 49 43
47 40 50 20 31 45 13 23 26 14 55 10 51
39 58 12 8 33 29 22 34 19 59 15 57 38
Sorted deck
8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46
47 48 49 50 51 52 53 54 55 56 57 58 59
Explanation / Answer
==> Use a for loop to initialize the deck...deck[0]=8, deck[1]=9,..deck[51]=59.
int i,counter=8;
for(i=0;i<52;i++)
{
deck[i]= counter;
counter++;
}
==>Then use a for loop,going from index1 to index2. In that loop sum up the card values found between [index1..index2]
for(i=index1;i<=index2;i++)
{
sum+=deck[i];
}
==> Shuffle the cards using the approach shown in the video
There is no video available, so unable to provide the code.
==> Sort the deck of cards using the code shown in the video
//selection sort
for (int j = 0; j < deck.length - 1; j++)
{
int index1 = j;
for (int k = j + 1; k < deck.length; k++){
if (deck[k] < deck[index1]){
index1 = k;//searching for lowest index
}
}
int smallerNumber = deck[index1];
deck[index1] = deck[j];
deck[j] = smallerNumber;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.