Question: //Card package question1; /** * A Card object encapsulates a standard playing card in terms * of 4 suits and 13 ranks. Each card can

//Card package question1; /** * A Card object encapsulates a standard playing//Card package question1; /** * A Card object encapsulates a standard playing card in terms * of 4 suits and 13 ranks. Each card can also be described in * terms of an index in the range 0 to 51: *

 * index 0 to 12: A-C, 2-C, 3-C, ..., K-C * index 13 to 25: A-D, 2-D, 3-D, ..., K-D * index 26 to 38: A-H, 2-H, 3-H, ..., K-H * index 39 to 51: A-S, 2-S, 3-S, ..., K-S

* The index, rank (0-12) and suit (0-3) are related by *

 * index = 13*suit + rank * suit = index / 13 * rank = index % 13

*/ public class Card { private static String[] ranks = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; private static String rankLetter = "A23456789TJQK"; private static String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" }; private static String suitLetter = "CDHS"; private int rank; // 0 to 12 private int suit; // 0 to 3 private int index; // 0 to 51 private String rankName; // "Ace", "Two", ... "King" private String suitName; // "Clubs" ... "Spades" private String cardName; // e.g. A-C for ace of clubs /** * Construct a card given its rank and suit. * @param rank card rank in range 0 (Ace) to 12 (King) * @param suit card suit in range 0 (Clubs} to 3 (Spades) */ public Card(int rank , int suit) { this.rank = rank; this.suit = suit; index = 13*suit + rank; rankName = ranks[rank]; suitName = suits[suit]; cardName = rankLetter.substring(rank,rank+1) + "-" + suitLetter.substring(suit,suit+1); } /** * Construct a card given its index. * @param index card index in the range 0 (A-C) to 51 (K-S) */ public Card(int index) { this.index = index; rank = this.index % 13; suit = this.index / 13; rankName = ranks[rank]; suitName = suits[suit]; cardName = rankLetter.substring(rank,rank+1) + "-" + suitLetter.substring(suit,suit+1); } /** * Return the rank name of the card. * @return rank name of card: "Ace" to "King" */ public String getRankName() { return rankName; } /** * Return the suit name of the card. * @return suit name if card: "Clubs" to "Spades" */ public String getSuitName() { return suitName; } /** * Return the card name. * @return the card name in format A-C to K-S */ public String getCardName() { return cardName; } /** * Return a string representation of a card. * @return a string representation of a card. */ public String toString() { return cardName; } }

//CardDeck package question1;

import java.util.Random; /** * A CardDeck object represents a deck of 52 Card objects. */ public class CardDeck { private static final int DECK_SIZE = 52; private Card[] deck; private int topCardIndex; /** * Construct a deck of cards initialized in the standard order 0 to 51 */ public CardDeck() { deck = new Card[DECK_SIZE]; initialize(); } // put cards in standard order private void initialize() { topCardIndex = 0; for (int k = 0; k

//CardDeckTester package question1; /** * A simple test class for CardDeck class */ public class CardDeckTester { public void run() { CardDeck deck = new CardDeck(); System.out.println("Initilized deck:"); System.out.println(deck); System.out.println("Shuffled deck:"); deck.shuffle(); System.out.println(deck); // Deal a few cards Card card; for(int i = 0; i Consider the classes included in the q1 package. These include Card, CardDeck and CardDeckTester. Card represents a playing card, while CardDeck is a simple collection class that represents ... wait for it... a deck of cards. The CardDeck class uses an array and you should take a minute to review how it works. We've discussed in the lectures how a linked list is a data structure "engine". Your task in this question is to re-work the CardDeck class to use Java's built-in LinkedLis t collection class instead of the array that is there now. Here are a few considerations: You need to change the data field in CardDeck to a LinkedList (don't forget any necessary import statements). The constructor needs to be rewritten. Start with an empty LinkedList and add all 52 cards in order. Look at how the CardDeck constructor works now to know what order they should be put in. Keep in mind that a Card takes a single integer as an argument that determines both the suit and the rank. Shuffle() needs to be rewritten. You'll need to find a way to randomize the cards without losing any of them. You could try using an iterator to grab a card from a random location then moving to a new random location in the list and adding the card back in - but that's just one possible approach. Deal() needs a bit of work too - as with the current method, you need to remove the top card and return it. The top Card must leave the list - we don't deal cards AND keep them in the deck unless we are playing at a crooked table. cards InDeck() needs to return the number of cards left in the deck. This -could- be used when dealing to figure out if there are enough cards left to deal. Empty() should return true if there are no cards left in the deck. toString() should be rewritten. Take a look at the existing one. It prints out the cards left in the deck. You don't need to worry about the rank and suit, they are part of the Card class. I suggest an Iterator here to walk through the LinkedList. Just to be clear - ALL of your modifications should be done in the CardDeck class. Do NOT modify Card or CardTester. The markers will run CardTester to make sure that your class works properly. Again, the idea here is to recognize that we can change the engine of a data structure without changing its external interface (i.e., it still works the same). As you are modifying CardDeck, comment out some of the lines in CardDeckTester so that it's not trying to test methods you haven't adjusted yet

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!