Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ask me anything Java Programming: Create a class for a single playing card. a er

ID: 3804363 • Letter: A

Question

Ask me anything Java Programming: Create a class for a single playing card. a ere is the UML. Card Private data members are ur design +Card(rank:int, suit:TBD) +getSuit0: int (or char) +getRank0: int tsetCard (rank:int, suit(TBDO avoid tsetRandom0:void +compare (c Card): int +compare (cl:Card, c2:Card) int ttoString0: String Features Data members can be ofyour design. The public member methods should act like the rank and suit of a real card, but how you keep track is your choice. The other members are as you would expect, with the following: setRandom will set the card's value to a random rank and suit toString0 will return a string with the card represented any way that is reasonable-rank, suit) Create a driver program that will thoroughly test your card class. Then, use your card class to create a deck class. The constructor should initialize the deck (52 cards to all the cards in a deck. Here is a possible start for the UML Deck Deck Card +Decko +deal0: Card tnumLefto:int +shuffle 0: void +toString0 The constructor should create the anray of 52 unique cards. The other methods act as you would expect You may deviate from the UML as you see fit, as long as the public members behave as expected Write a driver program that thoroughly tests your deck class. Sc19 PM 3/4/2017 R2

Explanation / Answer


/**
* A class called Card that represents the card object.
* */
//Card.java
import java.util.Random;
public class Card
{
   //instance variables
   private char suit;
   private int rank;

   //default Constructor
   public Card()
   {
       suit=' ';
       rank=0;
   }

   //Constructor
   public Card(char suit,int rank)
   {
       this.suit=suit;
       this.rank=rank;
   }

   public void setSuit(char suit)
   {
       this.suit=suit;
   }
   public char getSuit()
   {
       return suit;
   }
   /*
   * The setrank method takes rank as argument
   * and set rank only if rank is in range of 1 and
   * 13 otherwise set 1 to rank.
   * */
   public void setrank(int rank)
   {
       if(rank<1 || rank>13)
           this.rank=1;
       else
           this.rank=rank;
   }
   public int getrank()
   {
       return rank;
   }
  
   public void setRandom()
   {
       Random r=new Random();
       rank=r.nextInt(13)+1;
      
       char suitvalues[]={'H','S','C','D'};
       suit=suitvalues[r.nextInt(4)+1];
      
   }  
   //Returns string representation of Card object
   public String toString()
   {      
       return rank+" of "+suit;
   }
}//end of Card class

-----------------------------------------------------------------------------------
///DeckOfCards.java
import java.util.Random;
public class Deck
{
   //Card array
   private Card[] cards;
   // number of cards in deck
   private static final int SIZE_OF_DECK = 52;
   // random number generator
   private Random rand = new Random();
   private char suitvalues[]={'H','S','C','D'};  
   private int numberOfCard;
   // constructor initializes the deck array of type Card
   public Deck()
   {
       numberOfCard=0;
       cards = new Card[52];
        numberOfCard = 0;
        for(int suit = 0; suit <suitvalues.length; suit++)
        {
            for(int value = 0; value <13; value++)
            {
                cards[numberOfCard] = new Card(suitvalues[suit],value);
                numberOfCard++;
            }
        }      
   }

   // shuffle the deck cards
   public void shuffle()
   {      
       // for each Card, pick another random Card (0-51) and swap them
       for (int index = 0; index < cards.length; index++)
       {
           // select a random number between 0 and 51
           int randIndex = rand.nextInt(SIZE_OF_DECK);
           // swap temp with rand index
           Card temp = cards[index];      
           cards[index] = cards[randIndex];
           cards[randIndex] = temp;          
       }
   }
  
   public Card deal() {      
       int remove=numberOfCard-1;
       numberOfCard--;
        return cards[remove];
    }

   /*Override the toString method that returns
   52 deck of cards */
   @Override
   public String toString() {
       String str="";
       for (int i = 0; i < numberOfCard; i++)
       {
           if((i+1)%4==0)
               str+=cards[i]+" ";
           else
               str+=cards[i]+" | ";
       }
       return str;
   }
} // end class DeckOfCards

-------------------------------------------------------------------------------------------

/**
* The java driver program that tests the Deck
* class deal, shuffle and toString methods.
* */
//Driver.java
public class Driver {  
   public static void main(String[] args) {
      
       //Create an object of Deck class
       Deck deck=new Deck();      
       System.out.println("Deck of Cards");
       //print ot console
       System.out.println(deck.toString());      
      
       System.out.println("Deal Card");
       //calling deal
       System.out.println(deck.deal());  
       System.out.println("Deck of Cards");
       System.out.println(deck.toString());
       System.out.println("Deal Card");
       //calling deal
       System.out.println(deck.deal());
       System.out.println("Deck of Cards");
       System.out.println(deck.toString());
      
   }
  
}
-------------------------------------------------------------------------------------------
Sample Output:
Deck of Cards
0 of H | 1 of H | 2 of H | 3 of H
4 of H | 5 of H | 6 of H | 7 of H
8 of H | 9 of H | 10 of H | 11 of H
12 of H | 0 of S | 1 of S | 2 of S
3 of S | 4 of S | 5 of S | 6 of S
7 of S | 8 of S | 9 of S | 10 of S
11 of S | 12 of S | 0 of C | 1 of C
2 of C | 3 of C | 4 of C | 5 of C
6 of C | 7 of C | 8 of C | 9 of C
10 of C | 11 of C | 12 of C | 0 of D
1 of D | 2 of D | 3 of D | 4 of D
5 of D | 6 of D | 7 of D | 8 of D
9 of D | 10 of D | 11 of D | 12 of D

Deal Card
12 of D
Deck of Cards
0 of H | 1 of H | 2 of H | 3 of H
4 of H | 5 of H | 6 of H | 7 of H
8 of H | 9 of H | 10 of H | 11 of H
12 of H | 0 of S | 1 of S | 2 of S
3 of S | 4 of S | 5 of S | 6 of S
7 of S | 8 of S | 9 of S | 10 of S
11 of S | 12 of S | 0 of C | 1 of C
2 of C | 3 of C | 4 of C | 5 of C
6 of C | 7 of C | 8 of C | 9 of C
10 of C | 11 of C | 12 of C | 0 of D
1 of D | 2 of D | 3 of D | 4 of D
5 of D | 6 of D | 7 of D | 8 of D
9 of D | 10 of D | 11 of D |
Deal Card
11 of D
Deck of Cards
0 of H | 1 of H | 2 of H | 3 of H
4 of H | 5 of H | 6 of H | 7 of H
8 of H | 9 of H | 10 of H | 11 of H
12 of H | 0 of S | 1 of S | 2 of S
3 of S | 4 of S | 5 of S | 6 of S
7 of S | 8 of S | 9 of S | 10 of S
11 of S | 12 of S | 0 of C | 1 of C
2 of C | 3 of C | 4 of C | 5 of C
6 of C | 7 of C | 8 of C | 9 of C
10 of C | 11 of C | 12 of C | 0 of D
1 of D | 2 of D | 3 of D | 4 of D
5 of D | 6 of D | 7 of D | 8 of D
9 of D | 10 of D |

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote