Question: Answer the questions below in your .java comments: Questions: 1. Explain in your own words the relationship between a deck and a card. Use complete

Answer the questions below in your .java comments: Questions: 1. Explain in your own words the relationship between a deck and a card. Use complete sentences, and write at an AP level. 2. Consider the deck initialized with the statements below. How many cards does the deck contain? String[] ranks = {"jack", "queen", "king"}; String[] suits = {"blue", "red"}; int[] pointValues = {11, 12, 13}; Deck d = new Deck(ranks, suits, pointValues); 3. The game of Twenty-One is played with a deck of 52 cards. Ranks run from ace (highest) down to 2 (lowest). Suits are spades, hearts, diamonds, and clubs as in many other games. A face card has point value 10; an ace has point value 11; point values for 2, , 10 are 2, , 10, respectively. Specify the contents of the ranks, suits, and pointValues arrays so that the statement Deck d = new Deck(ranks, suits, pointValues); initializes a deck for a Twenty-One game. 4. Does the order of elements of the ranks, suits, and pointValues arrays matter?

public class Card {

/** * String value that holds the suit of the card */ private String suit;

/** * String value that holds the rank of the card */ private String rank;

/** * int value that holds the point value. */ private int pointValue;

/** * Creates a new Card instance. * * @param cardRank a String value * containing the rank of the card * @param cardSuit a String value * containing the suit of the card * @param cardPointValue an int value * containing the point value of the card */ public Card(String cardRank, String cardSuit, int cardPointValue) { //initializes a new Card with the given rank, suit, and point value rank = cardRank; suit = cardSuit; pointValue = cardPointValue; }

/** * Accesses this Card's suit. * @return this Card's suit. */ public String suit() { return suit; }

/** * Accesses this Card's rank. * @return this Card's rank. */ public String rank() { return rank; }

/** * Accesses this Card's point value. * @return this Card's point value. */ public int pointValue() { return pointValue; }

/** Compare this card with the argument. * @param otherCard the other card to compare to this * @return true if the rank, suit, and point value of this card * are equal to those of the argument; * false otherwise. */ public boolean matches(Card otherCard) { return otherCard.suit().equals(this.suit()) && otherCard.rank().equals(this.rank()) && otherCard.pointValue() == this.pointValue(); }

/** * Converts the rank, suit, and point value into a string in the format * "[Rank] of [Suit] (point value = [PointValue])". * This provides a useful way of printing the contents * of a Deck in an easily readable format or performing * other similar functions. * * @return a String containing the rank, suit, * and point value of the card. */ @Override public String toString() { return rank+" of "+suit+" (pv = "+pointValue+")"; } }

import java.util.List; import java.util.ArrayList;

/** * The Deck class represents a shuffled deck of cards. * It provides several operations including * initialize, shuffle, deal, and check if empty. */ public class Deck {

/** * cards contains all the cards in the deck. */ private List cards;

/** * size is the number of not-yet-dealt cards. * Cards are dealt from the top (highest index) down. * The next card to be dealt is at size - 1. */ private int size;

/** * Creates a new Deck instance.
* It pairs each element of ranks with each element of suits, * and produces one of the corresponding card. * @param ranks is an array containing all of the card ranks. * @param suits is an array containing all of the card suits. * @param values is an array containing all of the card point values. */ public Deck(String[] ranks, String[] suits, int[] values) { /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */ cards = new ArrayList(); for (int j=0; j

/** * Determines if this deck is empty (no undealt cards). * @return true if this deck is empty, false otherwise. */ public boolean isEmpty() { return size == 0; }

/** * Accesses the number of undealt cards in this deck. * @return the number of undealt cards in this deck. */ public int size() { return size; }

/** * Deals a card from this deck. * @return the card just dealt, or null if all the cards have been * previously dealt. */ public Card deal() { if (isEmpty()) return null; size--; Card c=cards.get(size); return c; }

public String toString() { // =============== ACE TO KING =============== // *******UNDEALT CARDS (still in deck!)******* String rtn="size = "+size+" Undealt cards (A-K): "; for (int k=size-1; k>=0; k--) { rtn=rtn+cards.get(k); if (k!=0) rtn=rtn+", "; // Insert carriage returns so entire deck is visible on console. if ( (size-k)%2 == 0) rtn=rtn+" "; } // ******* DEALT CARDS (K-A) ******* rtn=rtn+" Dealt cards (K-A): "; for (int k=cards.size()-1; k>=size; k--) { rtn=rtn+cards.get(k); if (k!=size) rtn=rtn+", "; // Insert carriage returns so entire deck is visible on console. if ( (k-cards.size())%2 == 0) rtn=rtn+" "; }

// =============== ACE TO KING =============== // ******* UNDEALT CARDS (A-K) ******* rtn=rtn+" ===== size = "+size+" ==>Undealt cards (A-K): "; for (int k=0; kDealt cards (A-K): "; for (int k=size; k

}

import java.util.List; import java.util.ArrayList;

public class DeckTester {

/** * The main method in this class checks the Deck operations for consistency. * @param args is not used. */ public static void main(String[] args) { /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */

String[] Suits = { "Clubs","Diamonds","Hearts","Spades"}; String[] Ranks = { "Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"}; int[] cardVals = { 1,2,3,4,5,6,7,8,9,10,10,10,10 };

Deck x = new Deck(Ranks,Suits,cardVals); System.out.println(x.toString()); System.out.println("**** Deal 5 Cards ****"); for (int i = 0; i < 5; i++) { System.out.println(" deal: " + x.deal()); } System.out.println(x.toString()); } }

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!