Question

In: Computer Science

Create new Deck object Run testDeck( ) Should get four columns (13 rows each), each column...

  1. Create new Deck object
  2. Run testDeck( )
    • Should get four columns (13 rows each), each column with a unique suit and ranked ace to king
  3. Run shuffle( ) then run testDeck( ) again
    • Should get four NEAT columns,(13 rows each) with random numbers and suits in each column
  4. Create another Deck object
  5. Run testDeal( ) with a parameter of 5
    • Should get Ace, 2, 3, 4, 5 of spades as the “hand”
  6. Now run shuffle( ) then run testDeal( ) again with a parameter of 10
    • Should get 10 random cards

Deck Class

public class Deck
{
private final String[] suits = {"Spades", "Hearts", "Clubs", "Diamonds"};
private final String[] faces = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

private Card[] cards;
private int topCard;
  
/**
* Construct a deck of cards and initialize the topCard to -1.
*/
public Deck()
{
}
/**
* deal() returns the next card or null if the deck is empty.
* @return next Card in the deck.
*/
public Card deal()
{
topCard++;// set topCard to the next card
if(topCard < 52)
{   
return cards[topCard];
}
else
{
return null;
}
}
  
/**
* shuffle() randomly generates a sequence of cards for the card array
*/
public void shuffle()
{
topCard = -1;// reset the top card
int nextNumber;
boolean[] available = new boolean[52];// used to select random #'s between 0 and 51 without replacement

for(int i = 0; i < 52; i++)
{
available[i] = true;//all #'s between 0 and 51 are available
}
  
for(int i = 0; i < 52; i++)
{
nextNumber = (int)(Math.random()*52);// select a # from 0 to 51
while(!available[nextNumber])//while nextNumber is not available (i.e. this number has already been used)
{
nextNumber = (int)(Math.random()*52);//try a different number until you find an unused one
}
available[nextNumber] = false;// this number is taken
cards[i] = new Card(suits[nextNumber/13], faces[nextNumber%13]);
}
}

/**
* Print out the entire deck for testing purposes.
*/
public void testDeck()
{

  
  
}
/*
Write a method called "testDeck" that uses a loop (whichever
type of loop you think is appropriate) to print out all cards
in the deck (Card array) in four NEAT columns.
When "testDeck" is called on a brand new deck each COLUMN should
contain one suit with that suit's cards in order. For
example: the first column should contain all of the
spades in order: Ace Spades, 2 Spades, 3 Spades, ..., King Spades.
The second column should contain all of the clubs in
order:Ace Clubs, 2 Clubs, 3 Clubs, ... and so on.
When called on a shuffled deck cards will be random but still
in NEAT columns.
WHEN YOU ARE FINISHED WRITING THIS METHOD REMOVE THIS COMMENT
*/
  
/**
* Print out a subset of the deck to test the deal method.
*/
public void testDeal(int numberOfCards)
{

}
/*
Write a method called "testDeal" that takes a single integer
parameter called "numberOfCards". Create a local ArrayList of Cards (i.e. ArrayList<Card>)
called "hand".
Use an index variable, a while loop, and the deal() method
to fill the "hand" with a number of cards equal to numberOfCards.
Next use a loop (whichever type of loop you think is appropriate) to go through the hand
and print out all of the cards in a single NEAT column.
(Note: this method uses an ArrayList of Cards, not an array. Pay attention
to the difference.)
WHEN YOU ARE FINISHED WRITING THIS METHOD REMOVE THIS COMMENT
*/

Card Class

public class Card
{
private String suit;
private String face;

/**
* Constructor for objects of class Card.
*/
public Card(String suit, String face)
{
this.suit = suit;
this.face = face;
}

/**
* Returns the suit of the card
*/
public String getSuit()
{
return suit;
}
  
/**
* Returns the face of the card
*/
public String getFace()
{
return face;
}
  
/**
* Returns the suit and face of the card as a single String
*/
public String getCard()
{
return face + "\t" + suit;
}
}

Solutions

Expert Solution

// Card.java

public class Card
{
   private String suit;
   private String face;

   /**
   * Constructor for objects of class Card.
   */
   public Card(String suit, String face)
   {
       this.suit = suit;
       this.face = face;
   }

   /**
   * Returns the suit of the card
   */
   public String getSuit()
   {
       return suit;
   }
  
   /**
   * Returns the face of the card
   */
   public String getFace()
   {
       return face;
   }
  
   /**
   * Returns the suit and face of the card as a single String
   */
   public String getCard()
   {
       return face + "\t" + suit;
   }
}
// end of Card.java

// Deck.java

import java.util.ArrayList;

public class Deck
{
   private final String[] suits = {"Spades", "Hearts", "Clubs", "Diamonds"};
   private final String[] faces = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

   private Card[] cards;
   private int topCard;
  
   /**
   * Construct a deck of cards and initialize the topCard to -1.
   */
   public Deck()
   {
       // create an array of 52 cards
       cards = new Card[52];
       topCard = 0;
      
       // loop to populate the cards
       // loop over the suits
       for(int i=0;i<suits.length;i++)
       {
           // loop over the faces for each suit
           for(int j=0;j<faces.length;j++)
           {
               cards[topCard] = new Card(suits[i], faces[j]); // create a new card for ith suit and jth face and set it to topCard
               topCard++;
           }
       }
      
       topCard = -1; // set topCard to -1
   }
   /**
   * deal() returns the next card or null if the deck is empty.
   * @return next Card in the deck.
   */
   public Card deal()
   {
       topCard++;// set topCard to the next card
       if(topCard < 52)
       {   
           return cards[topCard];
       }
       else
       {
           return null;
       }
   }
  
   /**
   * shuffle() randomly generates a sequence of cards for the card array
   */
   public void shuffle()
   {
       topCard = -1;// reset the top card
       int nextNumber;
       boolean[] available = new boolean[52];// used to select random #'s between 0 and 51 without replacement
  
       for(int i = 0; i < 52; i++)
       {
           available[i] = true;//all #'s between 0 and 51 are available
       }
      
       for(int i = 0; i < 52; i++)
       {
           nextNumber = (int)(Math.random()*52);// select a # from 0 to 51
           while(!available[nextNumber])//while nextNumber is not available (i.e. this number has already been used)
           {
               nextNumber = (int)(Math.random()*52);//try a different number until you find an unused one
           }
           available[nextNumber] = false;// this number is taken
           cards[i] = new Card(suits[nextNumber/13], faces[nextNumber%13]);
       }
   }

   /**
   * Print out the entire deck for testing purposes.
   */
   public void testDeck()
   {
       int c = 0;
       // loop over the faces
       for(int i=0;i<faces.length;i++)
       {
           // loop over the suit for each face
           for(int j=0;j<suits.length;j++)
           {
               // display the card (so that unshuffled deck displays each COLUMN containing one suit with that suit's cards in order
               System.out.printf("%20s",cards[13*j+i].getFace()+" "+cards[13*j+i].getSuit());
               if((c+1)%4 == 0) // 4 cards have been displayed, output a new line
                   System.out.println();
               c++;
           }
          
       }
   }
   /*
   Write a method called "testDeck" that uses a loop (whichever
   type of loop you think is appropriate) to print out all cards
   in the deck (Card array) in four NEAT columns.
   When "testDeck" is called on a brand new deck each COLUMN should
   contain one suit with that suit's cards in order. For
   example: the first column should contain all of the
   spades in order: Ace Spades, 2 Spades, 3 Spades, ..., King Spades.
   The second column should contain all of the clubs in
   order:Ace Clubs, 2 Clubs, 3 Clubs, ... and so on.
   When called on a shuffled deck cards will be random but still
   in NEAT columns.
   WHEN YOU ARE FINISHED WRITING THIS METHOD REMOVE THIS COMMENT
   */

   /**
   * Print out a subset of the deck to test the deal method.
   */
   public void testDeal(int numberOfCards)
   {
       // create an array list of card
       ArrayList<Card> hand = new ArrayList<Card>();
      
       // loop to deal numberOfCards from deck to hand
       int i = 0;
      
       // loop to deal numberOfCards until there are cards in the deck
       while(i < numberOfCards && topCard < 52)
       {
           hand.add(deal());
           i++;
       }
      
       // loop to display numberOfCards dealt
       for(i=0;i<hand.size();i++)
       {
               // display the card
               System.out.printf("%20s\n",hand.get(i).getCard());
       }
       System.out.println();
   }
   /*
   Write a method called "testDeal" that takes a single integer
   parameter called "numberOfCards". Create a local ArrayList of Cards (i.e. ArrayList<Card>)
   called "hand".
   Use an index variable, a while loop, and the deal() method
   to fill the "hand" with a number of cards equal to numberOfCards.
   Next use a loop (whichever type of loop you think is appropriate) to go through the hand
   and print out all of the cards in a single NEAT column.
   (Note: this method uses an ArrayList of Cards, not an array. Pay attention
   to the difference.)
   WHEN YOU ARE FINISHED WRITING THIS METHOD REMOVE THIS COMMENT
   */
}

//end of Deck.java

// DeckDriver.java

public class DeckDriver {

   public static void main(String[] args) {

       // Create new Deck object
       Deck deck1 = new Deck();
       // Run testDeck( )
       System.out.println("Test Deck(Before Shuffle):");
       deck1.testDeck();
       // Run shuffle( ) then run testDeck( ) again
       deck1.shuffle();
       System.out.println("\nTest Deck(After Shuffle):");
       deck1.testDeck();
      
       // Create another Deck object
       Deck deck2 = new Deck();
       // Run testDeal( ) with a parameter of 5
       System.out.println("\n\nTest Deal(Before Shuffle):");
       deck2.testDeal(5);
       // Now run shuffle( ) then run testDeal( ) again with a parameter of 10
       deck2.shuffle();
       System.out.println("\nTest Deal(After Shuffle):");
       deck2.testDeal(10);
      
   }

}

//end of DeckDriver.java

Output:


Related Solutions

"Create a program that displays a table consisting of four rows and five columns. The first...
"Create a program that displays a table consisting of four rows and five columns. The first column should display the numbers 1 through 4. The second and sub-sequent columns should display the result of multiplying the number in the first column by the numbers 2 through 5. If necessary, create a new project named Introductory14 Project, and save it in the Cpp8\Chap08 folder. Enter the C++ instructions into a source file named Introductory14.cpp. Also enter appropriate comments and any additional...
1 – Create a webpage that contains a table with exactly three rows and two columns....
1 – Create a webpage that contains a table with exactly three rows and two columns. The first row will contain a table heading containing the name of a US National Park, that spans across all columns. Hint: use the colspan attribute inside the opening th tag Give the table heading an onmouseover that will change the text of the heading when it is moused over to read My Favorites Park! (Hint: use innerHTML). Use onmouseout to change it back....
1 – Create a webpage that contains a table with exactly three rows and two columns....
1 – Create a webpage that contains a table with exactly three rows and two columns. The first row will contain a table heading containing the name of a US National Park, that spans across all columns. Hint: use the colspan attribute inside the opening th tag Give the table heading an onmouseover that will change the text of the heading when it is moused over to read My Favorites Park! (Hint: use innerHTML). Use onmouseout to change it back....
2. Make a data frame consisting of 20 and 10 columns. Each column j should consist...
2. Make a data frame consisting of 20 and 10 columns. Each column j should consist of 20 values from a normal distribution with mean (i-1) and standard deviation 0.5j. For example, the third column should be normal(mean=2, sd=1.5). Using this data frame, do each of the following (using code, of course): a. Find the mean and standard deviation for each column. b. Write code that counts the number of columns for which the sample mean and sample standard deviation...
task A normal deck of cards has 52 cards, consisting of 13 each of four suits:...
task A normal deck of cards has 52 cards, consisting of 13 each of four suits: spades, hearts, diamonds, and clubs. Hearts and diamonds are red, while spades and clubs are black. Each suit has an ace, nine cards numbered 2 through 10, and three face cards. The face cards are a jack, a queen, and a king. Answer the following questions for a single card drawn at random from a well-shuffled deck of cards. a. What is the probability...
C++ Question Create a class for a card in a deck of playing cards. The object...
C++ Question Create a class for a card in a deck of playing cards. The object must contain methods for setting and retrieving the suit and the type of card (type of card meaning 2,3,4,5,6,7,8,9,10,J,Q,K,A). Separate the declaration of the class from its implementation by using the declaration in a header file and the implementation in a .cpp file. Also create a driver program and a makefile which complies the code and makes it able to run. This is more...
MATLAB Create a matrix named P which has 2000 rows and 100 columns of all zeros....
MATLAB Create a matrix named P which has 2000 rows and 100 columns of all zeros. Replace the zeros in rows 46-58 and columns 3-18 of matrix P from the previous question with ones.
In poker, there is a 52 card deck with 4 cards each of each of 13...
In poker, there is a 52 card deck with 4 cards each of each of 13 face values. A full house is a hand of 5 cards with 3 of one face value, and 2 of another. What is the probability that a random poker hand is a full house? You can leave your answer in terms of bionomial co-efficients and similar factors, but please explain each term.
Create a game of Connect Four using a two dimensional array (that has 6 rows and...
Create a game of Connect Four using a two dimensional array (that has 6 rows and 7 columns) as a class variable. It should be user friendly and be a two person game. It should have clear directions and be visually appealing. The code MUST have a minimum of 3 methods (other than the main method). The player should be able to choose to play again. **Code must be written in Java**
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should...
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should have the following properties: Movie Name Director Name Year Released WasSuccessful (this should be a boolean and at least 2 should be false) Genre Loop through all of the objects in Array If the movie is successful, display all the movie information on the page. These movies were a success: Title: Forrest Gump Year Realeased: 1994 Director: Robert Zemeckis Genre: Comedy
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT