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

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 stackA = new ArrayStack(Card.class); // Add the line below to create an empty queue of Cards via Morin's // ArrayQueue queueA = new ArrayQueue(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

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

ArrayStack stackA = new ArrayStack(Card.class);

// Create an empty queue of Cards via Morin's ArrayQueue class (done) ArrayQueue queueA = new ArrayQueue(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 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

} // end of Class Deck

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!