Using Java, please implement the Poker and PokerHand classes to provide the expected output using the CardDemo class.
Rules are the following:
- Deck and PockerHand should be Iterable (note that you may just reuse one of the
underlying iterators to provide this functionality)
- Poker should implement 2 methods checking for two possible poker hands , see the
comments inside the classes for details. Hint: remember how one can count the frequency
of words in text?
- whenever you pass references around, remember about differences between composition
and aggregation
- Card, Deck, PockerHand should override the Object’s toString method in order to
produce the desired output from the main method provided.
- try to avoid code duplication as much as possible.
public class Card {
private CardValue cardValue;
private CardSuit suit;
public Card (CardValue cardValue, CardSuit suit)
{
this.cardValue = cardValue;
this.suit = suit;
}
public CardValue getCardValue()
{
return cardValue;
}
public CardSuit getSuit()
{
return suit;
}
@Override
public String toString(){
return cardValue+" "+suit;
}
}
public enum CardValue {
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
}
public enum CardSuit {
HEARTS, SPADES, CLUBS, DIAMONDS
}
public class CardDeck implements Iterable {
ArrayList deck;
public CardDeck ()
{
deck = new ArrayList<>();
for (CardSuit suit: CardSuit.values())
for (CardValue value: CardValue.values())
deck.add(new Card(value, suit));
}
public void shuffle(){
Random r = new Random();
int n = 50 + r.nextInt(100);
while (n>0){
n--;
int i = r.nextInt(52);
int j = r.nextInt(52);
Card temp = deck.get(i);
deck.set(i, deck.get(j));
deck.set(j, temp);
}
}
public Collection draw5Cards (){
ArrayList cards = new ArrayList<>();
int n = 5;
while (n>0){
n--;
cards.add(deck.get(0));
deck.remove(0);
}
return cards;
}
public Card drawCard (){
Card card = deck.get(0);
deck.remove(0);
return card;
}
public int size(){
return deck.size();
}
@Override
public String toString (){
String s = "";
for (Card card: deck){
s += card.toString() + "\n";
}
return s;
}
@Override
public Iterator iterator() {
return deck.iterator();
}
}
Poker
public class Poker {
private Poker(){};
/**
* Checks if a hand contains a Pair:
https://en.wikipedia.org/wiki/List_of_poker_hands#One_pair
* @param hand
* @return true if the hand contains exactly one pair
of cards with the same value (rank)
*
*/
public static boolean hasPair(PokerHand hand){
//TODO
}
/**
* Checks if a hand contains a Pair:
https://en.wikipedia.org/wiki/List_of_poker_hands#Two_pair
* @param hand
* @return true if the hand contains exactly two pairs
of cards with the same value (rank)
* NOTE: Full house
https://en.wikipedia.org/wiki/List_of_poker_hands#Full_house
hands
* are to be excluded
*/
public static boolean has2Pairs(PokerHand hand){
//TODO
}
}
PokerHand
public class PokerHand implements Iterable {
/**
* Creates a new hand from a collection of 5
cards
* the hand must contain exactly 5 cards, and they must
be distinct
* @param hand
* @throws IllegalArgumentException if the conditions
above a violated
*/
public PokerHand(Collection hand) {
//TODO
}
/**
* Creates a new hand from 5 separate card
objects
* there should be exactly 5 parameters, and they must
be distinct
* @param hand 5 card objects
* @throws IllegalArgumentException if the conditions
above a violated
*/
public PokerHand(Card... hand) {
//TODO
}
/**
* @return a list of cards currently held
*/
@SuppressWarnings("unchecked")
public List getHand (){
//TODO
}
@Override
public String toString (){
//TODO
}
@Override
public Iterator iterator() {
//TODO
}
}
Hi,
Please find the below code according to problem statement.
-----------------------------------------------------------------------------------
public class Card {
private CardValue cardValue;
private CardSuit suit;
public Card (CardValue cardValue, CardSuit suit)
{
this.cardValue = cardValue;
this.suit = suit;
}
public CardValue getCardValue()
{
return cardValue;
}
public CardSuit getSuit()
{
return suit;
}
@Override
public String toString(){
return cardValue+" "+suit;
}
}
-----------------------------------------------------------------------------------
public enum CardValue {
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
}
-----------------------------------------------------------------------------------
public enum CardSuit {
HEARTS, SPADES, CLUBS, DIAMONDS
}
-----------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
public class CardDeck implements Iterable {
ArrayList<Card> deck;
public CardDeck()
{
deck = new ArrayList<>();
for (CardSuit suit : CardSuit.values())
for (CardValue value : CardValue.values())
deck.add(new Card(value, suit));
}
public void shuffle() {
Random r = new Random();
int n = 50 + r.nextInt(100);
while (n > 0) {
n--;
int i = r.nextInt(52);
int j = r.nextInt(52);
Card temp = deck.get(i);
deck.set(i, deck.get(j));
deck.set(j, temp);
}
}
public Collection<Card> draw5Cards() {
ArrayList<Card> cards = new ArrayList<>();
int n = 5;
while (n > 0) {
n--;
cards.add(deck.get(0));
deck.remove(0);
}
return cards;
}
public Card drawCard() {
Card card = deck.get(0);
deck.remove(0);
return card;
}
public int size() {
return deck.size();
}
@Override
public String toString() {
String s = "";
for (Card card : deck) {
s += card.toString() + "\n";
}
return s;
}
@Override
public Iterator iterator() {
return deck.iterator();
}
}
-----------------------------------------------------------------------------------
import java.util.List;
import java.util.stream.Collectors;
public class Poker {
private Poker() {
};
/**
* Checks if a hand contains a Pair:
*
https://en.wikipedia.org/wiki/List_of_poker_hands#One_pair
*
* @param hand
* @return true if the hand contains exactly one pair
of cards with the same
* value (rank)
*
*/
public static boolean hasPair(PokerHand hand) {
boolean a1, a2, a3, a4;
List<Card> cards =
hand.getHand();
if (cards.size() != 5) {
return
false;
}
List<String> cardValues =
cards.stream().map(c ->
c.getCardValue().toString()).sorted()
.collect(Collectors.toList());
a1 =
cardValues.get(0).equals(cardValues.get(1));
a2 =
cardValues.get(1).equals(cardValues.get(2));
a3 =
cardValues.get(2).equals(cardValues.get(3));
a4 =
cardValues.get(3).equals(cardValues.get(4));
return a1 | a2 | a3 | a4;
}
/**
* Checks if a hand contains a Pair:
*
https://en.wikipedia.org/wiki/List_of_poker_hands#Two_pair
*
* @param hand
* @return true if the hand contains exactly two pairs
of cards with the same
* value (rank) NOTE: Full house
*
https://en.wikipedia.org/wiki/List_of_poker_hands#Full_house
hands
* are to be excluded
*/
public static boolean has2Pairs(PokerHand hand)
{
boolean a1, a2, a3;
List<Card> cards =
hand.getHand();
if (cards.size() != 5) {
return
false;
}
List<String> cardValues =
cards.stream().map(c ->
c.getCardValue().toString()).sorted()
.collect(Collectors.toList());
a1 =
cardValues.get(0).equals(cardValues.get(1)) &&
cardValues.get(2).equals(cardValues.get(3));
a2 =
cardValues.get(0).equals(cardValues.get(1)) &&
cardValues.get(3).equals(cardValues.get(4));
a3 =
cardValues.get(1).equals(cardValues.get(2)) &&
cardValues.get(3).equals(cardValues.get(4));
return a1 || a2 || a3;
}
}
-----------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
public class PokerHand implements Iterable {
ArrayList<Card> hand;
/**
* Creates a new hand from a collection of 5 cards the
hand must contain exactly
* 5 cards, and they must be distinct
*
* @param hand
* @throws IllegalArgumentException if the conditions
above a violated
*/
public PokerHand(Collection<Card> hand) {
this.hand = new
ArrayList<>();
this.hand.addAll(hand);
}
/**
* Creates a new hand from 5 separate card objects
there should be exactly 5
* parameters, and they must be distinct
*
* @param hand 5 card objects
* @throws IllegalArgumentException if the conditions
above a violated
*/
public PokerHand(Card... hand) {
this.hand = new
ArrayList<>();
for (int i = 0; i < 5;
i++)
this.hand.add(hand[i]);
}
/**
* @return a list of cards currently held
*/
@SuppressWarnings("unchecked")
public List<Card> getHand() {
return hand;
}
@Override
public String toString() {
String s = "";
for (Card card : hand) {
s += card.toString() + "\n";
}
return s;
}
@Override
public Iterator iterator() {
return hand.iterator();
}
}
-----------------------------------------------------------------------------------
I hope this helps you!!
Thanks.
Get Answers For Free
Most questions answered within 1 hours.