Question

While using JAVA , solve this. There are three basic classes we'll need this week: Card:...

While using JAVA , solve this.

There are three basic classes we'll need this week:

  • Card: A class like the one presented in the modules, but with a few changes.
  • Hand: A class that represents the cards held by a single player.
  • Deck: A class that represents the source of the cards for dealing and, as the game progresses, the place from which players can receive new cards (say, as they pick cards "from the deck" or when future hands are to be dealt from the same deck). Recall this picture, which relates the Deck to the various Hands that it creates through the process called "dealing".

Here are eight cards, each of which contains both a value ('A', '2', '3', ... 'T', 'J', 'Q',' K') and a suit (spades ♠, hearts ♥, diamonds ♦, clubs ♣)
pic of cards

Notice that I am using the char 'T' to describe the value 10. (Ignore the Joker, which we will not need.)

The dealer uses a Deck object to deal Hand objects to the players. The dealer may or may not be a player who gets a hand of his own (poker dealers in casinos don't receive a hand, but most other games involve the dealer getting a hand).

pic of dealing


Card: The Card class has two obvious members:  value (a char) and suit (an enum). But we add a new boolean, errorFlag, which can inform a client that a card is in an illegal state. We'll want the usual constructors, mutators, accessors and toString() methods for the class. We only allow standard cards, like ('A', clubs), ('9', hearts) and ('T', diamonds), no jokers or other special cards.

Hand: As you can see, a Hand object usually contains several cards, so we'll need an array of Card objects (myArray) as the principal member of the Hand class. Since each game deals a different number of cards into its players hands, and even within a game the number of cards in a hand will increase or decrease, we must keep track of this with an int value (numCards). We'll need constructors, mutators, etc., of course. We'll also want a way for the hand to receive a card (from the deck or somewhere else), and play a card (to the table or to another player). These two methods will be called takeCard() and playCard(), respectively. Since this class has no information about the game being played, it always puts new cards received by takeCard() into the next available location of the array (index position numCards) and plays a card via playCard() from the highest occupied location (index position numCards - 1). The client game application would somehow prepare this highest position with the correct card to be played before calling Hand's playCard() method. This detail is not our concern.

Deck: A Deck object is the source of all cards. It's where the dealer gets cards to deal, and if a player takes an individual card after the deal, he takes it from the Deck object. Naturally, the primary member here is an array of Card objects, much like Hand. We'll call this member cards[]. A deck normally consists of a single pack of cards: 52 cards (four suits of 13 values each). However, some games use two, three or more packs. If a card game requires two packs, then the deck will consist of two full 52-card packs: 104 cards. (Many games throw away some cards before beginning. For example Pinochle wants all cards with values 8-and-below to be taken out of the deck, but we will not trouble ourselves with this complexity.) A newly instantiated deck will have a multiple of 52 cards and will contain all the standard cards, so the number of cards in a newly instantiated deck will be 52, 104, 156, ..., i.e., numPacks × 52.

Clearly, we need an int like Hand's numCards, to keep track of how many cards are actually in the cards[] array. To this end, we'll use topCard (not numCards), since a deck typically removes and delivers cards to players from the top-of-the-deck, and this is a convenient variable to use for the number of cards as well as the position of the top of the deck.

There are a few other useful members (numPacks, for example). In addition to the the usual constructors and accessors, we'll want a dealCard() to return and remove the card at the top of the deck (which may be received by a client and added to some player's hand), and a shuffle() to re-order the cards in a random fashion. Also, we'll need to restock the deck (init()) to the original full condition in preparation for a fresh deal (we would certainly not want to re-instantiate a new deck when we have a perfectly good one available: garbage collection, done by us or by the operating system, is a resource we do not abuse).

Phase 2: The Hand Class

Static Class Constants

Define a public int value like MAX_CARDS and set it to something like 50 or 100 so a runaway program can't try to create a monster array.

Private Member Data

   Card[] myCards;
   int numCards;

Public Methods

  • Hand() - a default constructor.
  • void resetHand() - remove all cards from the hand (in the simplest way).
  • boolean takeCard(Card card) - adds a card to the next available position in the myCards array. This is an object copy, not a reference copy, since the source of the Card might destroy or change its data after our Hand gets it -- we want our local data to be exactly as it was when we received it.
  • Card playCard() - returns and removes the card in the top occupied position of the array.
  • String toString() - a stringizer that the client can use to display the entire hand.
  • Accessor for numCards.
  • Card inspectCard(int k) - Accessor for an individual card. Returns a card with errorFlag = true if k is bad.

Test of Hand class

Create between two and five explicit Card objects and one Hand object. Use takeCard() on these few cards (resulting in many, unavoidable "duplicates" in the hand) in a loop to populate the hand until the maximum allowable cards is met (use this criterion to end the loop). Display the hand using toString(). Next,  play each card in a loop, until the hand is empty. Display the card played as it is played, and finally, display the (now empty)  hand, verifying that no cards remain. At some point in your program, test inspectCard() with both legal and illegal int arguments.

Example Test Run of Hand Class

/* -------------------------------------------------------------------------
Hand full
After deal
Hand =  ( 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hear
ts, 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 
of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 of clu
bs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T 
of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clu
bs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 
of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hea
rts, 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3
 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 of cl
ubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T
 of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of cl
ubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9
 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of he
arts, 3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 
3 of clubs, T of clubs, 9 of hearts, 3 of clubs, T of clubs, 9 of hearts, 3 of c
lubs, T of clubs, 9 of hearts, 3 of clubs )

Testing inspectCard()
9 of hearts
** illegal **
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs
Playing 9 of hearts
Playing T of clubs
Playing 3 of clubs

After playing all cards
Hand =  (  )
----------------------------------------------------------------------- */
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code implemented in Java

Note: Comments are written for code explanation

Filename: Hand.java

public class Hand {
   Card[] myCards;
   static int numCards;
   public static final int MAX_CARDS = 30;
  
   public Hand(){
       myCards = new Card[MAX_CARDS];
   }
  
   public Hand(int size){
      
       if(size == 0 || size > MAX_CARDS){
           System.out.println("Invalid hand");
       }else{
           myCards = new Card[size];
       }
      
   }
  
   public void resetHand(){
       this.myCards = new Card[MAX_CARDS];
   }
  
   public boolean takeCard(Card card){
      
       // find the next available position in myCards
       int index = numCards() ;
      
       // if index is greater than max cards return false
       if(index >= MAX_CARDS){
           return false;
       }
       // insert card into last available index
       else{
           myCards[index] = card;
           return true;
       }
      
      
      
   }
  
   public String toString(){
      
       String hand = "";
  
       // Iterate through myCards array
       for(int i = 0; i<numCards(); i++){
          
           hand = hand + myCards[i].toString() + ",";
       }
      
       // Remove trailing comma
       hand = hand.replaceAll(",$", "");
      
       // Insert output string into parenthesis
       hand = "Hand = (" + hand + ")";
       return hand;
   }
  
   public int numCards(){
       int index = 0;
       for(int i = 0; i < myCards.length; i++){
           if(myCards[i] != null){
               index++;
           }
       }
       numCards = index;
       return numCards;
   }
  
   public Card playCard(){
       int lastCard = numCards() - 1;
       Card drawCard = myCards[lastCard];
       myCards[lastCard] = null;
      
       return drawCard;
      
   }
  
   public Card inspectCard(int k){
      
       Card dummy = new Card('C', Suit.Clubs);
      
       if(k > numCards() -1 ){
           return dummy;
       }
      
       return myCards[k];
      
      
   }

}
public class Hand { Card[] myCards; static int numCards; public static final int MAX_CARDS = 30; public Hand(){ myCards = new for(int i = 0; i<numCards(); i++){ hand = hand + myCards[i].toString() + ,; // Remove trailing comma hand = hand.replaceAll

Filename: Suit.java

public enum Suit {Clubs, Diamonds, Hearts, Spades}

1 public enum Suit {Clubs, Diamonds, Hearts, Spades}

Filename: Card.java

public class Card {
   char value;
   Suit suit;
   boolean errorFlag;
  
   // Constructor with default values
   public Card(){
      
       this.value = 'A';
       this.suit = Suit.Spades;
      
       set('A', Suit.Spades);
   }
  
   // Constructor for custom values
   public Card(char value, Suit suit){
      
       // evaluate all characters as uppercase
       value = Character.toUpperCase(value);
      
       set(value, suit);
      
   }
  
   // stringizer to display the values
  
   public String toString(){
      
       if(errorFlag == true){
           return "** illegal **";
       }
       else{
           return value + " of " + suit;
       }
      
   }
  
   private static boolean isValid(char value, Suit suit){
      
       boolean isValid = false;
       char[] lookup = {'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};
      
       for(char i : lookup){
           if(i == value){
               isValid = true;
           }
       }
      
       return isValid;
   }
  
   public boolean set(char value, Suit suit){
      
       // check if value is valid
       if(isValid(value, suit)){
           this.value = value;
           this.suit = suit;
           errorFlag = false;
       }
       else{
           errorFlag = true;
       }
      
       return errorFlag;
   }
  
   public boolean equals(Card card){
       if(card.value == this.value && card.suit == this.suit){
           return true;
       }
       else{
           return false;
       }
   }
  
   // Accessors
  
   public Suit getSuit(){
       return this.suit;
   }
  
   public char getValue(){
       return this.value;
   }
  
   public boolean getErrorFlag(){
       return this.errorFlag;
   }
}
public class Card { char value; Suit suit; boolean errorFlag: // Constructor with default values public Card(){ this.value = if(i == value) { isValid = true; return isValid; public boolean set(char value, Suit suit){ // check if value is valid if(isV

Filename: handTest.java

public class handTest {

   public static void main(String[] args) {
      
       // Run for phase 1:
      
      
       System.out.println("/* -----------BEGIN PHASE ONE------------------- ");
      
       Card card1 = new Card('a', Suit.Diamonds);
       Card card2 = new Card();
       Card card3 = new Card('C', Suit.Diamonds);
      
      
       System.out.println(card1.toString());
       System.out.println(card2.toString());
       System.out.println(card3.toString());
      
       card2.set('G', Suit.Hearts);
       card3.set('7', Suit.Hearts);
      
       System.out.println("\n");
      
       System.out.println(card1.toString());
       System.out.println(card2.toString());
       System.out.println(card3.toString());
      
       System.out.println("-------------END PHASE ONE------------------- */\n");
      
       // Run for phase 2:
       System.out.println("/* -----------BEGIN PHASE TWO------------------- ");
      
       Hand hand = new Hand();
      
       Card card4 = new Card('A', Suit.Diamonds);
       Card card5 = new Card('3', Suit.Clubs);
       Card card6 = new Card('T', Suit.Spades);
       Card card7 = new Card('7', Suit.Clubs);
       Card card8 = new Card('6', Suit.Hearts);
      
       hand.takeCard(card4);
       hand.takeCard(card5);
      
       System.out.println("Taking two cards initially...");
       System.out.println(hand.toString());
      
       hand.playCard();
      
       System.out.println("Here is the hand after playing one card: " + hand.toString() + "\n");
      
       System.out.println("Testing inspectCard()");
       System.out.println(hand.inspectCard(0)); // Good Card
       System.out.println(hand.inspectCard(33)); // Out of Bounds
       System.out.println(hand.inspectCard(1)); // Null card
       System.out.println("\n");
      
       System.out.println("Now filling the hand with the remaining cards ...\n");
       while(hand.numCards() < hand.MAX_CARDS){
          
           hand.takeCard(card6);
           hand.takeCard(card7);
           hand.takeCard(card8);
           hand.takeCard(card4);
           hand.takeCard(card5);
          
       }
       System.out.println("Hand full\nAfter deal");
       System.out.println(hand.toString());
      
       while(hand.numCards() > 0){
           System.out.println("Playing " + hand.playCard());
       }
      
       System.out.println("After playing all cards");
       System.out.println(hand.toString());
      
       System.out.println("-------------END PHASE TWO------------------- */\n");
   }

}
17 public class handTest { public static void main(String[] args) { // Run for phase 1: System.out.println(/* -----------BEG Card card4 = new Card (A, Suit.Diamonds); Card card5 = new Card(3, Suit.Clubs); Card card6 = new Card(T, Suit.Spades);

Working Code Output:

/* -----------BEGIN PHASE ONE-------------------
A of Diamonds
A of Spades
** illegal **


A of Diamonds
** illegal **
7 of Hearts
-------------END PHASE ONE------------------- */

/* -----------BEGIN PHASE TWO-------------------
Taking two cards initially...
Hand = (A of Diamonds,3 of Clubs)
Here is the hand after playing one card: Hand = (A of Diamonds)

Testing inspectCard()
A of Diamonds
** illegal **
** illegal **


Now filling the hand with the remaining cards ...

Hand full
After deal
Hand = (A of Diamonds,T of Spades,7 of Clubs,6 of Hearts,A of Diamonds,3 of Clubs,T of Spades,7 of Clubs,6 of Hearts,A of Diamonds,3 of Clubs,T of Spades,7 of Clubs,6 of Hearts,A of Diamonds,3 of Clubs,T of Spades,7 of Clubs,6 of Hearts,A of Diamonds,3 of Clubs,T of Spades,7 of Clubs,6 of Hearts,A of Diamonds,3 of Clubs,T of Spades,7 of Clubs,6 of Hearts,A of Diamonds)
Playing A of Diamonds
Playing 6 of Hearts
Playing 7 of Clubs
Playing T of Spades
Playing 3 of Clubs
Playing A of Diamonds
Playing 6 of Hearts
Playing 7 of Clubs
Playing T of Spades
Playing 3 of Clubs
Playing A of Diamonds
Playing 6 of Hearts
Playing 7 of Clubs
Playing T of Spades
Playing 3 of Clubs
Playing A of Diamonds
Playing 6 of Hearts
Playing 7 of Clubs
Playing T of Spades
Playing 3 of Clubs
Playing A of Diamonds
Playing 6 of Hearts
Playing 7 of Clubs
Playing T of Spades
Playing 3 of Clubs
Playing A of Diamonds
Playing 6 of Hearts
Playing 7 of Clubs
Playing T of Spades
Playing A of Diamonds
After playing all cards
Hand = ()
-------------END PHASE TWO------------------- */

Working Code Output Screenshot:

<terminated > Foothill [Java Application) C:\Program Files\Vava\jre-10.0.2\bin\javaw.exe (Mar 22, 2020, 9:38:55 AM) ----BEGINNow filling the hand with the remaining cards ... Hand full After deal Hand = (A of Diamonds, T of Spades, 7 of Clubs, 6 of H

If you like my answer, hit thumbs up. Thank you.

Add a comment
Know the answer?
Add Answer to:
While using JAVA , solve this. There are three basic classes we'll need this week: Card:...
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
  • CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...

    CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties an integer for the rank (1 (corresponding to Ace) ,2,3, 13 (correspond ing to King) and suit (Spades, Hearts, Diamonds, or Clubs). Make the suit an enumerated data type. Include getters and setters and a method that tests if a card is valid. Write a class named Deck whose instances are full...

  • Program 4: C++ The Game of War The game of war is a card game played by children and budding comp...

    Program 4: C++ The Game of War The game of war is a card game played by children and budding computer scientists. From Wikipedia: The objective of the game is to win all cards [Source: Wikipedia]. There are different interpretations on how to play The Game of War, so we will specify our SMU rules below: 1) 52 cards are shuffled and split evenly amongst two players (26 each) a. The 26 cards are placed into a “to play” pile...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! !!!!!!!!!!!!!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!!!!!!!!!!!!!!!!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in new deck order. (This is the order of a deck of cards new from the box.) The 2 Array lists...

  • 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...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • War—A Card game Playing cards are used in many computer games, including versions of such classic...

    War—A Card game Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. War:    Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is...

  • IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card...

    IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that...

  • 4. A group of students are playing a card game. The game uses a well-shuffled deck...

    4. A group of students are playing a card game. The game uses a well-shuffled deck of 56 playing cards. 52 of the cards are exactly as described in your textbook. However, there are also 4 Jokers. This means that there are 56 cards: 14 are hearts, 14 are diamonds, 14 are clubs, and 14 are spades. A hand of cards consists of Eight of the cards. Find the number of different hands that contain: a. At least 6 Diamonds....

  • Write a class named Card which will represent a card from a deck of cards. A...

    Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that randomly shuffles the cards in the...

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
ADVERTISEMENT