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
[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...
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: 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...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Please ask the user to input a low range and a high range and then print...
Please ask the user to input a low range and a high range and then print the range between them. Add printRang method to BST.java that, given a low key value, and high key value, print all records in a sorted order whose values fall between the two given keys from the inventory.txt file. (Both low key and high key do not have to be a key on the list). ****Please seperate the information in text file by product id,...