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 // 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 // Create an empty queue of Cards via Morin's ArrayQueue class (done) ArrayQueue // 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 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
Get step-by-step solutions from verified subject matter experts
