Question

In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at...

In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method ONLY in the Deck class. After testing the shuffle method, use the Deck toString method to “see” the cards after every shuffle.

Deck:

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<Card> 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 <code>Deck</code> instance.<BR>
   * 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) {
       cards = new ArrayList<Card>();
       for (int j = 0; j < ranks.length; j++) {
           for (String suitString : suits) {
               cards.add(new Card(ranks[j], suitString, values[j]));
           }
       }
       size = cards.size();
       shuffle();
   }


   /**
   * 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;
   }

   /**
   * Randomly permute the given collection of cards
   * and reset the size to represent the entire deck.
   */
   public void shuffle() {
for( int x = size - 1; x >= 0; x-- ) {
int y = (int)(Math.random() * x);
Card k = cards.get(y);
cards.set(y, cards.get(x));
cards.set(x, k);
}
   }

   /**
   * 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;

} else {

size--;

Card c = cards.get(size);

return c;

}

}
   /**
   * Generates and returns a string representation of this deck.
   * @return a string representation of this deck.
   */
   @Override
   public String toString() {
       String rtn = "size = " + size + "\nUndealt cards: \n";

       for (int k = size - 1; k >= 0; k--) {
           rtn = rtn + cards.get(k);
           if (k != 0) {
               rtn = rtn + ", ";
           }
           if ((size - k) % 2 == 0) {
               // Insert carriage returns so entire deck is visible on console.
               rtn = rtn + "\n";
           }
       }

       rtn = rtn + "\nDealt cards: \n";
       for (int k = cards.size() - 1; k >= size; k--) {
           rtn = rtn + cards.get(k);
           if (k != size) {
               rtn = rtn + ", ";
           }
           if ((k - cards.size()) % 2 == 0) {
               // Insert carriage returns so entire deck is visible on console.
               rtn = rtn + "\n";
           }
       }

       rtn = rtn + "\n";
       return rtn;
   }
}

DeckTester:

/**
* This is a class that tests the Deck class.
*/
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) {
       String[] ranks = {"jack", "queen", "king"};
       String[] suits = {"blue", "red"};
       int[] pointValues = {11, 12, 13};
       Deck d = new Deck(ranks, suits, pointValues);

       System.out.println("**** Original Deck Methods ****");
       System.out.println(" toString:\n" + d.toString());
       System.out.println(" isEmpty: " + d.isEmpty());
       System.out.println(" size: " + d.size());
       System.out.println();
       System.out.println();

       System.out.println("**** Deal a Card ****");
       System.out.println(" deal: " + d.deal());
       System.out.println();
       System.out.println();

       System.out.println("**** Deck Methods After 1 Card Dealt ****");
       System.out.println(" toString:\n" + d.toString());
       System.out.println(" isEmpty: " + d.isEmpty());
       System.out.println(" size: " + d.size());
       System.out.println();
       System.out.println();

       System.out.println("**** Deal Remaining 5 Cards ****");
       for (int i = 0; i < 5; i++) {
           System.out.println(" deal: " + d.deal());
       }
       System.out.println();
       System.out.println();

       System.out.println("**** Deck Methods After All Cards Dealt ****");
       System.out.println(" toString:\n" + d.toString());
       System.out.println(" isEmpty: " + d.isEmpty());
       System.out.println(" size: " + d.size());
       System.out.println();
       System.out.println();

       System.out.println("**** Deal a Card From Empty Deck ****");
       System.out.println(" deal: " + d.deal());
       System.out.println();
       System.out.println();

       /* *** TO BE COMPLETED *** */
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Optional;

public class Card {
private String rank;
private String suit;
private int value;

public Card(String rank, String suit, int value) {
this.rank = rank;
this.suit = suit;
this.value = value;
}

public Card(String suit, int value) {
this.suit = suit;
this.value = value;
}

public String getRank() {
return rank;
}

public void setRank(String rank) {
this.rank = rank;
}

public String getSuit() {
return suit;
}

public void setSuit(String suit) {
this.suit = suit;
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

@Override
public String toString() {
if (rank == null) {
return suit + " " + value;
} else {
return rank + " " + suit + " " + value;
}

}
}


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

/*
Here Deck Class is used to represent Shuffled Deck of cards
It is supported by various operations That includeds Intilize , shuffle, and deak and check if empty
*/
public class Deck {

// Here Cards conta all cards in deck

private List<Card> cards;

/*
       Here Size is the NO of not-yet-dealt cards
In default cards are from top to down (highest index to lowest index)
By default The card is at size -1
*/
private int size;


/*
Here Creates a new Deck instance
It pairs each element of rank with suits,
and it produces one of the corresponding card.

The array @param rank containing all of he card rank.
The array @param suits containing all of the card suits.
The array @param values containing all of the card point values.
*/
public Deck(String[] rank, String[] suits, int[] values) {
cards = new ArrayList<Card>();
for (int j = 0; j < rank.length; j++) {
for (String suitString : suits) {
cards.add(new Card(rank[j], suitString, values[ j]));
}
}

if (rank.length < values.length) {
for ( int x = rank.length; x < values.length; x++){
for (String suitString : suits) {
cards.add(new Card(suitString, values[x]));
}
}
}
size = cards.size();
shuffle();
}


/*
It checks if this empty or not (no default cards)
   and returns true if deck is empyt else return false
*/
public boolean isEmpty() {
return size == 0;
}

/*
It Accesses the no of undealt cards in this deck.

and returns the no of undealt cards in this deck.
*/
public int size() {
return size;
}

/*
       It Randomly permutes given set (collection) of cards
       and to repersent entire deck it reset the size
*/
public void shuffle() {
for (int x = size - 1; x >= 0; x--) {
int y = (int) (Math.random() * x);
Card k = cards.get(y);
cards.set(y, cards.get(x));
cards.set(x, k);
}
}

/*
Deals card from deck.
and return the card just dealt, or reurns null if all the cards have been previously dealt.
*/
public Card deal() {

if (isEmpty()) {

return null;

} else {

size--;

Card c = cards.get(size);

return c;

}

}

/*
It Generates and returns a string representation of this deck.

*/
@Override
public String toString() {
String rtn = "Size = " + size + "\n Undealt cards are : \n";

for (int k = size - 1; k >= 0; k--) {
rtn = rtn + cards.get(k);
if (k != 0) {
rtn = rtn + ", ";
}
if ((size - k) % 2 == 0) {
// Insert carriage is returns So Entire deck visible
rtn = rtn + "\n";
}
}

rtn = rtn + "\nDealt cards are: \n";
for (int k = cards.size() - 1; k >= size; k--) {
rtn = rtn + cards.get(k);
if (k != size) {
rtn = rtn + ", ";
}
if ((k - cards.size()) % 2 == 0) {
// Insert carriage returns so Entire deck is Visible
rtn = rtn + "\n";
}
}

rtn = rtn + "\n";
return rtn;
}
}

/*
This is a class tests the Deck class.
*/
class DeckTester {


public static void main(String[] args) {
String[] rank = {"jack", "queen", "king"};
String[] suits = {"blue", "red"};
int[] pointValues = {11, 12, 13};
Deck d = new Deck(rank, suits, pointValues);

System.out.println("** Original Deck Methods are ");
System.out.println(" toString :\n" + d.toString());
System.out.println(" isEmpty : " + d.isEmpty());
System.out.println(" size is : " + d.size());
System.out.println();
System.out.println();

System.out.println(" Deal a Card ");
System.out.println(" deal : " + d.deal());
System.out.println();
System.out.println();

System.out.println(" Deck Methods After 1 Card Dealt is ");
System.out.println(" toString:\n" + d.toString());
System.out.println(" isEmpty: " + d.isEmpty());
System.out.println(" size: " + d.size());
System.out.println();
System.out.println();

System.out.println("Deal Remaining 5 Cards are ");
for (int i = 0; i < 5; i++) {
System.out.println(" deal: " + d.deal());
}
System.out.println();
System.out.println();

System.out.println(" Deck Methods After All Cards Dealt are*");
System.out.println(" toString:\n" + d.toString());
System.out.println(" isEmpty: " + d.isEmpty());
System.out.println(" size: " + d.size());
System.out.println();
System.out.println();

System.out.println("Deal a Card From Empty Deck is ");
System.out.println(" deal: " + d.deal());
System.out.println();
System.out.println();

/* * TO BE COMPLETED * */

String[] newrank = {"ace", "king", "queen", "jack"};
String[] newSuits = {"Spades", "Diamonds", "Hearts", "Clubs"};
int[] newPointValues = {14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2};

Deck packDeck = new Deck(newrank, newSuits, newPointValues);

System.out.println("Original Deck Methods are");
System.out.println(" toString:\n" + packDeck.toString());
System.out.println(" isEmpty: " + packDeck.isEmpty());
System.out.println(" size: " + packDeck.size());
System.out.println();
System.out.println();

System.out.println("Deal a Card ");
System.out.println(" deal: " + packDeck.deal());
System.out.println();
System.out.println();

System.out.println(" Deck Methods After 1 Card Dealt is ");
System.out.println(" toString:\n" + packDeck.toString());
System.out.println(" isEmpty: " + packDeck.isEmpty());
System.out.println(" size: " + packDeck.size());
System.out.println();
System.out.println();

System.out.println(" Deal Remaining 51 Cards are ");
for (int i = 0; i < 51; i++) {
System.out.println(" deal: " + packDeck.deal());
}
System.out.println();
System.out.println();

System.out.println(" Deck Methods After All Cards Dealt are");
System.out.println(" toString:\n" + packDeck.toString());
System.out.println(" isEmpty: " + packDeck.isEmpty());
System.out.println(" size: " + packDeck.size());
System.out.println();
System.out.println();

System.out.println(" Deal a Card From Empty Deck is ");
System.out.println(" deal: " + packDeck.deal());
System.out.println();
System.out.println();
}
}

Thank You :-)

Add a comment
Know the answer?
Add Answer to:
In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack...

    Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card {    private final String face; // face of card ("Ace", "Deuce", ...)    private final String suit; // suit of card ("Hearts", "Diamonds", ...)    // two-argument constructor initializes card's face and suit    public...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

  • Java Write a complete program that implements the functionality of a deck of cards. In writing...

    Java Write a complete program that implements the functionality of a deck of cards. In writing your program, use the provided DeckDriver and Card classes shown below. Write your own Deck class so that it works in conjunction with the two given classes. Use anonymous objects where appropriate. Deck class details: Use an ArrayList to store Card objects. Deck constructor: The Deck constructor should initialize your ArrayList with the 52 cards found in a standard deck. Each card is a...

  • C Programming The following code creates a deck of cards, shuffles it, and deals to players....

    C Programming The following code creates a deck of cards, shuffles it, and deals to players. This program receives command line input [1-13] for number of players and command line input [1-13] for number of cards. Please modify this code to deal cards in a poker game. Command line input must accept [2-10] players and [5] cards only per player. Please validate input. Then, display the sorted hands, and then display the sorted hands - labeling each hand with its...

  • I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

    I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

  • JAVAFX ONLY PROGRAM!!!!! SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS. DIRECTIONS ARE BELOW: DIRECTIONS: The main...

    JAVAFX ONLY PROGRAM!!!!! SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS. DIRECTIONS ARE BELOW: DIRECTIONS: The main point of the exercise is to demonstrate your ability to use various types of nested classes. Of course, sorting is important as well, but you don’t really need to do much more than create the class that does the comparison. In general, I like giving you some latitude in how you design and implement your projects. However, for this assignment, each piece is very...

  • Design and implement a class Q that uses Q.java as a code base. The queue ADT...

    Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...

  • In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These...

    In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT