I. Complete the missing imlementation of the Card class Implement Card(int,int),
ID: 3753042 • Letter: I
Question
I. Complete the missing imlementation of the Card class Implement Card(int,int), one of the constructors in the Card class so that they can handle exceptions (as outlined in the beginning file Deck.java). Complete the missing code in the member functions (as outlined in the beginning file Deck.java). Run test1 to demonstrate your implementation is correct.
II. Create and manipulate a stack/queue of Card objects Follow the instructions given from the starting file Deck.java, complete the missing code for test2 and test3.
III. Use appropriate data structures to simulate card manipulation Follow the instructions given from the starting file Deck.java, complete the missing code for test4.
/**
* File: Card.java
**/
// DO NOT MODIFY the following import statement
import java.util.concurrent.ThreadLocalRandom; // for the function myRandInt
/* Instructions:
* Follow the guidelines given in the comments, complete the implementation
* of the Card class
**/
public class Card {
private int rank; // the rank of the Card
private int suit; // the suit of the Card
/**
* Construct a valid playing Card
* @param aRank
* the rank of the Card
* @param aSuit
* the suit of the Card
* precondition
* aRank must be in the range from 0 to 12 (both inclusive)
* aSuit must be in the range from 0 to 3 (both inclusive)
* postcondition
* The Card has been initialized with a valid rank and a valid suit
* @exception IllegalArgumentException
* Indicates that rank is outside the given range when applicable
* Indicates that suit is outside the given range when applicable
**/
public Card (int aRank, int aSuit) throws IllegalArgumentException {
rank=0; suit=0; // remove this line when implementing this constructor
// ADD CODE HERE
}
/**
* constructor which assign valid rank and suit in a
* `random' manner, using the function myRandInt
*
**/
public Card () {
rank = myRandInt(0,12);
suit = myRandInt(0,3);
}
/**
* A getter function
* @return
* the suit of the Card
**/
public int getSuit(){
return 0; // change this to return the suit of the given card
}
/**
* A getter function
* @return
* the rank of the Card
**/
public int getRank(){
return 0; // change this to return the suit of the given card
}
/**
* Display the Card
* postcondition :
* the rank and the suit of the Card are displayed to screen,
* for example: ACES / SPADES
**/
public void display(){
// ADD code here
}
/**
* @param
* c1 is a Card
* @param
* c2 is a Card
* preconditions:
* first must be non-negative and first must be less than or equal to last
* @return
* an int randomly chosen from first (inclusive) to last (inclusive)
*
* needs to add the line import java.util.concurrent.ThreadLocalRandom;
**/
public static boolean same (Card c1, Card c2) {
return true; // Remove this line and add your code
}
/**
* @param first
* an int, the beginning of the range (inclusive)
* @param last
* an int, the end of the range (inclusive)
* @precondition
* 0 <= first <= last
* @return
* an int randomly chosen from first (inclusive) to last (inclusive)
* @require
* needs to add the line import java.util.concurrent.ThreadLocalRandom;
**/
// DO NOT MODIFY THIS FUNCTION
private int myRandInt(int first, int last){
return ThreadLocalRandom.current().nextInt(first, last + 1);
} // end myRandInt
} // end class Card
import java.util.concurrent.ThreadLocalRandom; // to use use randPermute
/* File: Deck.java
* Tests for the ArrayStack and ArrayQueue classes from Morin
* Tests for a Card class
*/
public class Deck {
/* Define Global constants */
static final int SHORT = 4;
static final int LONG = 13;
static final int ALL = 52;
public static void main (String [] argv) {
// The following shows how to create a stack (and queue) via Morin's code
// Add the line below to create an empty stack of Cards via Morin's
// ArrayStack<Card> stackA = new ArrayStack<Card>(Card.class);
// Add the line below to create an empty queue of Cards via Morin's
// ArrayQueue<Card> queueA = new ArrayQueue<Card>(Card.class);
// Initialize
// create a standard deck of cards
Card [] standard = new Card[ALL]; // Create a standard deck of cards
for (int i=0; i<ALL; i++) { // Fill the standard deck and display
standard[i] = new Card(i%13,i%4);
// standard[i].display(); // uncomment this line to show the standard deck
}
// create a shuffled deck of cards
int [] randIndex = randPermute(ALL); // Generate a random permutation of the array [0, .., ALL]
Card [] shuffled = new Card[ALL]; // Create a shuffled deck of cards from the standard deck
for (int i=0; i<ALL; i++) { // Fill the shuffled deck and display
shuffled[i] = new Card ((randIndex[i]%13),(randIndex[i]%4));
// shuffled[i].display(); // uncomment this line to show the shuffled deck
}
// end of initialize
// carry out the test
test1();
test2(standard, shuffled, ALL, SHORT);
test3(shuffled, standard, ALL, SHORT);
test4(standard, shuffled, ALL, SHORT);
} // end of Main
// test1 is for testing the constructor of Card class
// and other member functions with missing implementation
// (eg. same) in the Card class
// DO NOT MODIFY this function
public static void test1() {
System.out.println(" Begin of test 1: ");
Card [] test1 = new Card [SHORT];
for (int i=0; i<SHORT; i++) {
test1[i] = new Card (i+10,i+2);
}
if (Card.same(test1[0],test1[1]))
System.out.println("test1[0] and test1[1] are the same.");
else
System.out.println("test1[0] and test1[1] are different.");
if (Card.same(test1[2],test1[3]))
System.out.println("test1[2] and test1[3] are the same.");
System.out.println(" End of test 1: ");
}
// End of test1
// Follow the instructions given in the comments to implement test2
public static void test2 (Card [] fulldeck1, Card [] fulldeck2, int len, int sample) {
// len is the no of cards in fulldeck1 (and also fulldeck2, since each fulldeck has
// the same number of cards)
System.out.println(" Begin of test 2: ");
// Create an empty stack of Cards via Morin's ArrayStack class (done)
ArrayStack<Card> stackA = new ArrayStack<Card>(Card.class);
// Create an empty queue of Cards via Morin's ArrayQueue class (done)
ArrayQueue<Card> queueA = new ArrayQueue<Card>(Card.class);
// push the Cards fulldeck[0], ... , fulldeck[sample-1] to an empty stack
// enqueue the Card fulldeck[0], ... , fulldeck[sample-1] to an empty queue
// print something like
// "Display a sample of the first stack of cards from top to bottom:"
// when displaying the stack and queue in the steps below
// Display the stack of cards formed from top to bottom
// Display the queue of cards formed from front to end
// Repeat the same procedure above from the deck fulldeck2
System.out.println(" End of test 2. ");
}
public static void test3 (Card [] fulldeck1, Card [] fulldeck2, int len, int sample){
System.out.println(" Begin of test 3: ");
// Create q1, an empty queue of Cards via Morin's ArrayQueue class
// Create q2, an empty queue of Cards via Morin's ArrayQueue class
// put fulldeck1 into q1 in the same order
// put fulldeck2 into q2 in the same order
// Remove all the non-face cards from q1
// keep only the face cards (JACK, QUEEN, KING only) with suit ace in q1, maintain the original order
// keep only the aces in q2, maintain the original order
// display q1, from front to end, with the given heading
System.out.println("*** Display q1 ***");
// ADD code here
// display q2, from front to end, with the given heading
System.out.println("*** Display q2 ***");
// ADD code here
System.out.println(" end of test 3: ");
}
public static void test4 (Card [] fulldeck1, Card [] fulldeck2, int len, int sample){
System.out.println(" Begin of test 4: part 1 ");
// Create s1, an empty stack of Cards via Morin's ArrayStack class
// Create s2, an empty stack of Cards via Morin's ArrayStack class
// Create s3, an empty stack of Cards via Morin's ArrayStack class
// Create s4, an empty stack of Cards via Morin's ArrayStack class
// Distribute the cards in fulldeck1 to s1, s2, s3, s4 in the round robin fashion
// `push' each card on the top of the corresponding stack
// when it is done, show the top card from each stack in the order s1, s2, s3, s4
// ADD code here
System.out.print(" The top card from stack s1 is: ");
// ADD code here
System.out.print(" The top card from stack s2 is: ");
// ADD code here
System.out.print(" The top card from stack s3 is: ");
// ADD code here
System.out.print(" The top card from stack s4 is: ");
// ADD code here
System.out.println (" End of test 4: part 1 ");
System.out.println (" Begin of test 4: part 2 ");
// Repeat the same procedure for the fulldeck2. That is:
// Reset s1, s2, s3 and s4 to empty stacks of Cards via Morin's ArrayStack class
// Distribute the cards in fulldeck2 to s1, s2, s3, s4 in the round robin fashion
// `push' each card on the top of the corresponding stack
// when it is done, show the top card from each stack in the order s1, s2, s3, s4
// ADD code here
System.out.print(" The top card from stack s1 is: ");
// ADD code here
System.out.print(" The top card from stack s2 is: ");
// ADD code here
System.out.print(" The top card from stack s3 is: ");
// ADD code here
System.out.print(" The top card from stack s4 is: ");
// ADD code here
System.out.println(" End of test 4: part 2 ");
System.out.println(" End of test 4. ");
} // end test4
// DO NOT MODIFY THE CODE BELOW
public static int [] randPermute (int n) {
// n must be non-negative
int [] answer = new int[n];
for (int i=0; i<n; i++)
answer[i] =i;
int len = n;
while (len > 1){
swap(answer, getRandomIndex(0,len-1),len-1);
len--;} // end while
return answer;
} // end of randPermute
public static void swap(int [] a, int s, int t) {// 0<=s<=t<a.length
int temp = -1;
temp = a[s];
a[s] = a[t];
a[t] = temp;
} // end of swap
public static int getRandomIndex(int r, int s){ // requires 0 <= r <= s
// needs to add the line import java.util.concurrent.ThreadLocalRandom;
return ThreadLocalRandom.current().nextInt(r, s + 1);
} // end getRandomIndex
} // end of Class Deck
Explanation / Answer
public class Shuffler {
/**
* The number of consecutive shuffle steps to be performed in each call
* to each sorting procedure.
*/
private static final int SHUFFLE_COUNT = 1;
/**
* The number of values to shuffle.
*/
private static final int VALUE_COUNT = 4;
/**
* Tests shuffling methods.
* @param args is not used.
*/
public static void main(String[] args) {
System.out.println("Results of " + SHUFFLE_COUNT +
" consecutive perfect shuffles:");
int[] values1 = new int[VALUE_COUNT];
for (int i = 0; i < values1.length; i++) {
values1[i] = i;
}
for (int j = 1; j <= SHUFFLE_COUNT; j++) {
perfectShuffle(values1);
System.out.print(" " + j + ":");
for (int k = 0; k < values1.length; k++) {
System.out.print(" " + values1[k]);
}
System.out.println();
}
System.out.println();
System.out.println("Results of " + SHUFFLE_COUNT +
" consecutive efficient selection shuffles:");
int[] values2 = new int[VALUE_COUNT];
for (int i = 0; i < values2.length; i++) {
values2[i] = i;
}
for (int j = 1; j <= SHUFFLE_COUNT; j++) {
selectionShuffle(values2);
System.out.print(" " + j + ":");
for (int k = 0; k < values2.length; k++) {
System.out.print(" " + values2[k]);
}
System.out.println();
}
System.out.println();
}
/**
* Apply a "perfect shuffle" to the argument.
* The perfect shuffle algorithm splits the deck in half, then interleaves
* the cards in one half with the cards in the other.
* @param values is an array of integers simulating cards to be shuffled.
*/
public static void perfectShuffle(int[] values) {
int[] temp = new int[values.length];
int mid = (values.length + 1) / 2;
// Interleave elements 0 ... mid-1 with elements mid ... length-1
int unshuffledPos = 0;
for (int k = 0 ; k < mid; k++) {
temp[unshuffledPos] = values[k];
unshuffledPos += 2;
}
unshuffledPos = 1;
for (int k = mid; k < values.length; k++) {
temp[unshuffledPos] = values[k];
unshuffledPos += 2;
}
// Copy elements back to values
for (int k = 0; k < values.length; k++) {
values[k] = temp[k];
}
}
/**
* Apply an "efficient selection shuffle" to the argument.
* The selection shuffle algorithm conceptually maintains two sequences
* of cards: the selected cards (initially empty) and the not-yet-selected
* cards (initially the entire deck). It repeatedly does the following until
* all cards have been selected: randomly remove a card from those not yet
* selected and add it to the selected cards.
* An efficient version of this algorithm makes use of arrays to avoid
* searching for an as-yet-unselected card.
* @param values is an array of integers simulating cards to be shuffled.
*/
public static void selectionShuffle(int[] values) {
for (int k = values.length - 1; k > 0; k--) {
int pos = (int) (Math.random() * (k + 1)); // range 0 to k, inclusive
int temp = values[pos];
values[pos] = values[k];
values[k] = temp;
}
}
}