Add the following method below to the CardDeck class, and create a test driver to show...

50.1K

Verified Solution

Question

Programming

Add the following method below to the CardDeckclass, and create a test driver to show that they workcorrectly.

int cardsRemaining() //returns a count of the number of undealtcards remaining in the deck.

Complete in Java programming language.

// Models a deck of cards. Includes shuffling and dealing.

//----------------------------------------------------------------------

package Homework4;

import java.util.Random;

import java.util.Iterator;

import javax.swing.ImageIcon;

public class CardDeck {

public static final int NUMCARDS = 52;

protected ABList deck;

protected Iterator deal;

public CardDeck() {

deck = new ABList(NUMCARDS);

ImageIcon image;

for (Card.Suit suit : Card.Suit.values())

for (Card.Rank rank : Card.Rank.values()) {

image = new ImageIcon(\"./src/Homework4/cards/\" + suit + \"_\"

+ rank + \"_RA.gif\");

deck.add(new Card(rank, suit, image));

}

deal = deck.iterator();

}

public void shuffle()

// Randomizes the order of the cards in the deck.

// Resets the current deal.

{

Random rand = new Random(); // to generate random numbers

int randLoc; // random location in card deck

Card temp; // for swap of cards

for (int i = (NUMCARDS - 1); i > 0; i--) {

randLoc = rand.nextInt(i); // random integer between 0 and i -1

temp = deck.get(randLoc);

deck.set(randLoc, deck.get(i));

deck.set(i, temp);

}

deal = deck.iterator();

}

public boolean hasNextCard()

// Returns true if there are still cards left to be dealt;

// otherwise, returns false.

{

return (deal.hasNext());

}

public Card nextCard()

// Precondition: this.hasNextCard() == true

//

// Returns the next card for the current 'deal'.

{

return deal.next();

}

}

Answer & Explanation Solved by verified expert
3.8 Ratings (736 Votes)
package Homework4 import javautilRandom import javautilIterator import javaxswingImageIcon public class CardDeck public static final int NUMCARDS 52 int count0res0 protected ABList deck protected Iterator deal public CardDeck deck new    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students