You will be representing a deck of cards, there is a card class and a deck class
ID: 3537457 • Letter: Y
Question
You will be representing a deck of cards, there is a card class and a deck class, you must finish the methods in these classes. For extra credit you may finish the CardValidator class. The entire set of classes is available here.
card class:
package edu.bellevuecollege.cs211.playingcards;
public class Card
{
public enum Suit
{
CLUB("Club"), DIAMOND("Diamond"), HEART("Heart"), SPADE("Spade");
private String name;
private Suit(String name)
{
this.name = name;
}
public String toString()
{
return name;
}
}
public enum Value
{
TWO("Two", "2"),
THREE("Three", "3"),
FOUR("Four", "4"),
FIVE("Five", "5"),
SIX("Six", "6"),
SEVEN("Seven", "7"),
EIGHT("Eight", "8"),
NINE("Nine", "9"),
TEN("Ten", "10"),
JACK("Jack", "J"),
QUEEN("Queen", "Q"),
KING("King", "K"),
ACE("Ace", "A");
private String name;
private String representingChar;
private Value(String name, String representingChar)
{
this.name = name;
this.representingChar = representingChar;
}
/**
* @return the representingChar
*/
public String getRepresentingChar()
{
return representingChar;
}
public String toString()
{
return name;
}
}
public Card(Suit suit, Value value)
{
}
/**
* @return the suit
*/
public Suit getSuit()
{
return null;
}
/**
* @return the value
*/
public Value getValue()
{
return null;
}
/*
* (non-Javadoc)
* Return a string representation of a card.
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return null;
}
}
deck class:
package edu.bellevuecollege.cs211.playingcards;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import edu.bellevuecollege.cs211.playingcards.exceptions.DeckEmptyException;
public class Deck
{
public static int NUM_CARDS = 52;
private Card[] cards;
public static void main(String[] args)
{
}
public Deck()
{
}
/**
* Shuffles a deck of cards so that all 52 cards are randomly distributed.
*/
public void shuffle()
{
List<Card> temp = Arrays.asList(cards);
Collections.shuffle(temp);
cards = ((Card[])temp.toArray());
}
/**
* Draws a single card
*
* @return The card drawn out of the deck
*/
public Card draw() throws DeckEmptyException
{
return null;
}
public int cardsAvailable()
{
return 0;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return null;
}
public boolean isEmpty()
{
return false;
}
}
Explanation / Answer
//card class:
package edu.bellevuecollege.cs211.playingcards;
public class Card
{
public enum Suit
{
CLUB("Club"), DIAMOND("Diamond"), HEART("Heart"), SPADE("Spade");
private String name;
private Suit(String name)
{
this.name = name;
}
public String toString()
{
return name;
}
}
public enum Value
{
TWO("Two", "2"),
THREE("Three", "3"),
FOUR("Four", "4"),
FIVE("Five", "5"),
SIX("Six", "6"),
SEVEN("Seven", "7"),
EIGHT("Eight", "8"),
NINE("Nine", "9"),
TEN("Ten", "10"),
JACK("Jack", "J"),
QUEEN("Queen", "Q"),
KING("King", "K"),
ACE("Ace", "A");
private String name;
private String representingChar;
private Value(String name, String representingChar)
{
this.name = name;
this.representingChar = representingChar;
}
/**
* @return the representingChar
*/
public String getRepresentingChar()
{
return representingChar;
}
public String toString()
{
return name;
}
}
private Suit suit;
private Value value;
public Card(Suit suit, Value value)
{
this.suit=suit;
this.value=value;
}
/**
* @return the suit
*/
public Suit getSuit()
{
return this.suit;
}
/**
* @return the value
*/
public Value getValue()
{
return this.value;
}
/*
* (non-Javadoc)
* Return a string representation of a card.
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return (value.toString() +" of "+suit.toString());
}
}
//deck class:
package edu.bellevuecollege.cs211.playingcards;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import edu.bellevuecollege.cs211.playingcards.exceptions.DeckEmptyException;
public class Deck
{
public static int NUM_CARDS = 52;
private Card[] cards;
public static void main(String[] args)
{
//Create new deck of 52 cards
Deck D1 =new Deck();
//shuffle deck
D1.shuffle();
//show shuffled deck
System.out.println("Shuffled deck: "+D1);
//draw a top card
System.out.println("Drawn A Card: "+D1.draw());
}
public Deck()
{
//initialize deck with 52 cards
cards[]=new Card[NUM_CARDS];
int i=0;
for(Suit s: Suit.values())
{
for(Value v:value.values())
{
cards[i]=new Card(s,v);
i++;
}
}
}
/**
* Shuffles a deck of cards so that all 52 cards are randomly distributed.
*/
public void shuffle()
{
List<Card> temp = Arrays.asList(cards);
Collections.shuffle(temp);
cards = ((Card[])temp.toArray());
}
/**
* Draws a single card
*
* @return The card drawn out of the deck
*/
public Card draw() throws DeckEmptyException
{
List<Card> temp = Arrays.asList(cards);
//if empty throw exception,else remove card
if( temp.isEmpty()) { throw new DeckEmptyException(); }
else
{
//draw top card
Card c = temp.remove(0);
cards = ((Card[])temp.toArray());
return c;
}
return null;
}
public int cardsAvailable()
{
return cards.length;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
String ret="";
for(int i=0;i<cards.length;i++)
{ ret =ret+ cards[i].toString() +" "; }
return ret;
}
public boolean isEmpty()
{
if(cards.length == 0)
return true;
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.