Question

In: Computer Science

The goal of this exercise is to implement the shuffling algorithm from this chapter. 1. In...

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
   }
}

Solutions

Expert Solution

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() {
    }
}


Related Solutions

CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise...
CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise 19 of Chapter 1. Your program should allow the user to buy as many items as the user desires. Instructions for Exercise 19 of Chapter 1 have been posted below for your convenience. Exercise 19 Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is $200 or more, then the shipping and handling...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that can sort a singly-linked-list with Merge-sort. The program should read integers from file (hw-extra.txt) and create an unsorted singly-linked list. Then, the list should be sorted using merge sort algorithm. The merge-sort function should take the head of a linked list, and the size of the linked list as parameters. hw-extra.txt provided: 37 32 96 2 25 71 432 132 76 243 6 32...
1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt...
1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order. 2)You must implement a recursive Mergesort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order. My List.txt Values 7 3 4 1 4 4 9 9 4 8 4 5 3 9 2 3 7 0 6 4 4 5 0 1 9 2 1 7...
Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date...
Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check...
Exercise 1. Write an algorithm (pseudocode) to read a set of sales data items from standard...
Exercise 1. Write an algorithm (pseudocode) to read a set of sales data items from standard input and calculate and output their total and their average. Prompt user to enter number of data items. Exercise 2. Create a test data set to verify your algorithm. How many cases are needed? Explain. Write your test data set below for submission to the EOL dropbox upon completion of your lab. Number of items List data items Expected output Case 1: Exercise 3....
. (This is a version of Programming Project 2.1 from Chapter 2.) The Babylonian algorithm to...
. (This is a version of Programming Project 2.1 from Chapter 2.) The Babylonian algorithm to compute the square root of a positive number n is as follows: 1. Make a guess at the answer (you can pick n/2 as your initial guess). 2. Compute r = n / guess. 3. Set guess = (guess +r) / 2. 4. Go back to step 2 until the last two guess values are within 1% of each other. Write a JAVA program...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm,...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …, n; (4) 50 instances of n random numbers generated in the range of [1..n]. Note:...
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5,...
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5, number of resources 3 and the number of instances of each given resource is in available. You should complete the functionalities for safe state check and resource request processing. To Do 1. Complete the definition of isSafe function. The function take, the process array, 1D array of available resources, 2D array storing current allocation, and 2D array of current need. The function does not...
Analyze the algorithm experimentally. a)Implement the algorithm b)Let p be the string length at which your...
Analyze the algorithm experimentally. a)Implement the algorithm b)Let p be the string length at which your program takes 2 seconds to run, collect running times for your algorithm using the following string lengths: p/4, 2p/4, 3p/4, p, 5p/4, 6p/4, 7p/4, 2p. c)Generate your strings by reading the attached file, only reading as many characters as you need. d)Plot your results (x-axis is string length, y-axis should be time) e)Draw conclusions based on your graph. You may also need to plot...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page faults and page hits by considering the Frame size=4, ReferenceString:2 4 6 7 8 2 4 9 13 9 2 7 2 6 1 4 9 2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT