In: Computer Science
Add the following method below to the CardDeck class, and create a test driver to show that they work correctly.
int cardsRemaining() //returns a count of the number of undealt cards 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<Card> deck;
protected Iterator<Card> deal;
public CardDeck() {
deck = new ABList<Card>(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();
}
}
package Homework4;
import java.util.Random;
import java.util.Iterator;
import javax.swing.ImageIcon;
public class CardDeck {
public static final int NUMCARDS = 52;
int count=0,res=0;
protected ABList<Card> deck;
protected Iterator<Card> deal;
public CardDeck() {
deck = new ABList<Card>(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);
count=count+1;
}
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();
}
int cardsRemaining()
{
// veriable " res" is used to count the number of card remaining
if(this.hasNextCard() == true)
res=NUMCARDS-count;
return res;
}
}