Question
You are to write a Class Deck which emulates a full deck of playing cards. That is 4 suits (Clubs, Spades, Hearts, and Diamonds) and
You are to write a Class Deck which emulates a full deck of playing cards. That is 4 suits (Clubs, Spades, Hearts, and Diamonds) and 13 ranks (Ace, 2, 3, 4, 5, 6, 7, 8, 9, Jack, Queen, King) in each suit. This of course makes for a total of 52 playing cards in the deck.
Mandatory Instance variables:
private Card[] deck = new Card[52]; private int cardsDealt;
Mandatory Instance methods:
public Deck() // constructor
// set each element of deck to a unique Card object,
// and sets cardsDealt to zero.
public int getCardsDealt() // accessor
// return the value of cardsDealt.
private void setCardsDealt(int cardsDealt) // mutator
// sets cardsDealt specified value (cardsDealt)
public boolean emptyDeck()
// returns wheather or not all the cards in deck
// have already been dealt (cardsDealt == 52).
public void collectCards()
// set cardsDealt to zero.
public void collectCards(int cardCnt)
// decrement cardsDealt by cardCnt.
public Card dealCard()
// if emptyDeck() is false ...
// returns the card at location cardsDealt in deck,
// and increments cardsDealt by 1.
// else ...
// returns null
public void shuffleDeck()
// apply 100 random card swaps within deck
public void shuffleDeck(int swapCnt)
// apply swapCnt random card swaps within deck
You are also to write a Driver Class DeckDriver to test your Deck class.
Mandatory Functionality:
Your driver class must minimally print all the cards in the deck in the random order that they are dealt. Such as in Programs 1, 2, and 3.
Rules and Requirements:
All access of the instance variable cardsDealt, by the other instance methods is made via its accessor and mutator.
Rewrite your old programs.
what i have so far: Card.java:
public class Card implements Cloneable{
private String suit; private String rank;
//public Acesssors (Is this spelled wrong????)
public String getSuit() { return suit;
}
public String getRank() { return rank; }
//private Mutators private void setSuit(String suit) { this.suit = suit; }
private void setRank(String rank) { this.rank = rank; }
public Card(String suit, String rank) { this.suit = suit; this.rank = rank; }
public Card() { this.suit="Clubs"; this.rank="Ace"; }
@Override public String toString() { return rank +" of "+suit; }
public boolean equals(Card c) { if(this.getRank().equals(c.getRank()) && this.getSuit().equals(c.getSuit())) { return true; } else return false; }
public Card clone() throws CloneNotSupportedException { return this.clone(); } } Card Driver:
public class CardDriver {
public static void main(String args[]) {
Card deck[]=new Card[52]; String suit[]= {"Hearts","Diamonds","Clubs","Spades"}; String rank[]= {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"}; int i=0;
//Fills the deck for(int j=0;j<4;j++) { for(int k=0;k<13;k++) { deck[i++]=new Card(suit[j],rank[k]); } }
//Transpositioning 100 times for( i=0;i<100;i++) { //generating random indices int randomIndexOne=(int)(Math.random()*1000) % 52; int randomIndexTwo=(int)(Math.random()*1000) % 52;
//Swapping cards Card temp=deck[randomIndexOne]; deck[randomIndexOne]=deck[randomIndexTwo]; deck[randomIndexTwo]=temp; }
//Displaying contents of deck System.out.println(" Contents of the Deck:"); for(i=0;i<51;i++) { System.out.println(deck[i]); } for(i=0;i<51;i++) { if(deck[i].getRank().equals("Queen") && deck[i].getSuit().equals("Clubs")) { System.out.println(" Queen of Clubs is at Index "+ i); return; } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started