public class Chapter7Stub { public static void main(String[] args){ int[] deck =
ID: 3808174 • Letter: P
Question
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 Your code here //End of initialize block System.out.println("Initialized deck"); printDeck(deck); //Display the sum of card values between index1 and index2 // inclusively(passed in via command line arguments) int index1 = 0, index2 = 0, 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 Your code here //End of shuffle System.out.println("Shuffled deck"); printDeck(deck); //Use selection sort to order the cards in deck Your code here //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(); } }
Explanation / Answer
HI, Please find my code.
Please let me know in case of any issue.
import java.util.Random;
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 Your code here
Random random = new Random();
int rng = 59-8+1;
for(int i=0; i<deck.length; i++)
deck[i] = random.nextInt(rng) + 8;
//End of initialize block
System.out.println("Initialized deck");
printDeck(deck);
//Display the sum of card values between index1 and index2
// inclusively(passed in via command line arguments)
int index1 = 0, index2 = 0, sum = 0;
//Your code here
index1 = Integer.parseInt(args[0]);
index2 = Integer.parseInt(args[1]);
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 Your code here
for (int i = deck.length - 1; i > 0; i--)
{
int index = random.nextInt(i + 1);
// Simple swap
int a = deck[index];
deck[index] = deck[i];
deck[i] = a;
}
//End of shuffle
System.out.println("Shuffled deck");
printDeck(deck);
//Use selection sort to order the cards in deck Your code here
for (int i = 0; i < deck.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < deck.length; j++)
if (deck[j] < deck[index])
index = j;
int smallerNumber = deck[index];
deck[index] = deck[i];
deck[i] = smallerNumber;
}
//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();
}
}
/*
Sample run:
Initialized deck
37 9 36 28 52 52 42 55 10 35 38 27 28
24 51 12 30 47 18 30 27 34 52 14 20 37
48 46 8 17 20 58 57 28 53 42 45 24 9
22 32 38 57 34 55 31 28 56 59 45 19 59
Sum of card values between indexes 4 and 25 is 735
Shuffled deck
10 28 57 45 9 52 32 12 19 9 38 56 24
28 52 18 34 30 59 58 53 27 52 22 14 8
48 47 55 35 38 42 37 28 34 51 30 27 55
36 42 20 17 59 45 46 20 28 57 24 31 37
Sorted deck
8 9 9 10 12 14 17 18 19 20 20 22 24
24 27 27 28 28 28 28 30 30 31 32 34 34
35 36 37 37 38 38 42 42 45 45 46 47 48
51 52 52 52 53 55 55 56 57 57 58 59 59
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.