Question

Using Java, please implement the Poker and PokerHand classes to provide the expected output using the...

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
   }

}

Homework Answers

Answer #1

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.

Know the answer?
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
Similar Questions
Complete the implementation of the Card class. The two methods that you need to complete are...
Complete the implementation of the Card class. The two methods that you need to complete are compareTo and equals. CopareTo API: Compares this card to another card for order. This method imposes the following order on cards: Cards are first compared by rank. If the rank of this card is less than the rank of the other card then -1 is returned. If the rank of this card is greater than the rank of the other card then 1 is...
What's wrong with this code? #Java Why I am getting error in : int BusCompare =...
What's wrong with this code? #Java Why I am getting error in : int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses); public class TypeComparator implements Comparator<Type> { @Override public int compare(Type o1, Type o2) { // return o1.name.compareTo(o2.name); int NameCompare = o1.name.compareTo(o2.name); int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses); // 2-level comparison using if-else block if (NameCompare == 0) { return ((BusCompare == 0) ? NameCompare : BusCompare); } else { return NameCompare; } } } public class Type implements Comparable<Type> { int id; String name; int...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent...
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add the given Student to the StudentList. The removeStudent method should have one parameter of type String. The String is the email of the student to be removed from the StudentList. In Assign5Test.java do the following: Create a new StudentList containing 3 students. Print the info of all the Students in the StudentList using...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT