(Java Programming) public class Chapter7Stub { public static void main(String[]
ID: 3597039 • Letter: #
Question
(Java Programming)
public class Chapter7Stub {
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();
}
}
java 3 public class RandomShuffie f pablic static void main (Stringt] args) int[] number //generate an for (int i=1; -new int [10]; array 1(#numbers.length:1++) { numbersts-1-1 tor { (int System.out.print (numbersts]+) 1#0;>Explanation / Answer
Given below is the completed code. Please do rate the answer if it helped. Thank you
To indent code in eclipse, select code by pressing Ctrl+A and then Ctrl+i
public class Chapter7Stub {
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.
for(int i = 0; i < 52; i++)
deck[i] = 8 + i;
//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;
for(int i = index1; i <= index2; i++)
sum = sum + deck[i];
//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
for(int i = 0; i < deck.length; i++)
{
//swap the ith card with a randomly choosen index
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
//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
for(int i = 0; i < deck.length; i++)
{
int minIdx = i;
for(int j = i + 1; j < deck.length; j++)
{
if(deck[j] < deck[minIdx])
minIdx = j;
}
if(minIdx != i)
{
int temp = deck[i];
deck[i] = deck[minIdx];
deck[minIdx] = temp;
}
}
//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();
}
}
output
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
52 32 47 20 8 37 17 15 13 44 21 28 16
27 24 42 50 41 18 51 55 30 53 40 56 38
33 58 43 35 12 25 31 45 29 14 10 46 54
59 26 36 22 9 49 48 11 23 39 19 57 34
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.