In: Computer Science
The goal of this exercise is to implement the shuffling algorithm
from this chapter.
1. In the repository for this book, you should find the file named
Deck.java. Check that you can compile it in your environment.
2. Implement the randomInt method. You can use the nextInt method
provided by java.util.Random, which we saw in Section 7.6. Hint: To
avoid creating a Random object every time randomInt is invoked,
consider defining a class variable.
3. Write a swapCards method that takes two indexes and swaps the
cards at the given locations.
4. Fill in the shuffle method using the algorithm in Section
13.2.
Deck Info
import java.util.Arrays;
import java.util.Random;
public class Deck {
private Card[] cards;
public Deck() {
this.cards = new
Card[52];
int index = 0;
for (int suit = 0; suit
<= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
this.cards[index] = new Card(rank, suit);
index++;
}
}
}
public Deck(int n) {
this.cards = new
Card[n];
}
public Card[] getCards() {
return this.cards;
}
public void print() {
for (Card card :
this.cards) {
System.out.println(card);
}
}
public String toString() {
return
Arrays.toString(this.cards);
}
public void shuffle() {
}
private static int randomInt(int low, int high)
{
return 0;
}
private void swapCards(int i, int j) {
}
public void selectionSort() {
}
private int indexLowest(int low, int high)
{
return 0;
}
public Deck subdeck(int low, int high) {
Deck sub = new Deck(high
- low + 1);
for (int i = 0; i <
sub.cards.length; i++) {
sub.cards[i] = this.cards[low + i];
}
return sub;
}
private static Deck merge(Deck d1, Deck d2)
{
return null;
}
public Deck almostMergeSort() {
return this;
}
public Deck mergeSort() {
return this;
}
public void insertionSort() {
}
}
7.6 randomInt Example
public static int[] randomArray(int size) {
Random random = new Random();
int[] a = new int[size];
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(100);
}
return a;
}
Mentioned Shuffle Method
public void shuffle() {
for each index i {
// choose a random number between i and length -
1
// swap the ith card and the randomly-chosen
card
}
}
Program Screenshot for Indentation Reference:
Program code to copy:
import java.util.Arrays;
import java.util.Random;
public class Deck {
private Card[] cards;
// Random Number Generator
private static Random rand = new Random();
public Deck() {
this.cards = new
Card[52];
int index = 0;
for (int suit = 0; suit
<= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
this.cards[index] = new Card(rank, suit);
index++;
}
}
}
public Deck(int n) {
this.cards = new
Card[n];
}
public Card[] getCards() {
return this.cards;
}
public void print() {
for (Card card :
this.cards) {
System.out.println(card);
}
}
public String toString() {
return
Arrays.toString(this.cards);
}
public void shuffle() {
// shuffle the
decks
// loop over n-1
elements
for (int i = 0; i <
cards.length - 1; i++) {
// get a random j such than i <= j < n and swap
int j = randomInt(i, cards.length);
// swap cards
swapCards(i, j);
}
}
private static int randomInt(int low, int
high) {
// return a random
number p where low <= p < high
// by adding low and
getting a number between high - low
return low +
rand.nextInt(high - low);
}
private void swapCards(int i, int j) {
// swap
Card temp =
cards[i];
cards[i] =
cards[j];
cards[j] = temp;
}
public void selectionSort() {
}
private int indexLowest(int low, int high)
{
return 0;
}
public Deck subdeck(int low, int high)
{
Deck sub = new Deck(high
- low + 1);
for (int i = 0; i <
sub.cards.length; i++) {
sub.cards[i] = this.cards[low + i];
}
return sub;
}
private static Deck merge(Deck d1, Deck d2)
{
return null;
}
public Deck almostMergeSort() {
return this;
}
public Deck mergeSort() {
return this;
}
public void insertionSort() {
}
}