Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PokerHandEvaluator.java This class consists of several static methods that you w

ID: 3762313 • Letter: P

Question

PokerHandEvaluator.java
This class consists of several static methods that you will write. The prototypes for the methods are:

public static boolean hasPair(Card[] cards)

public static boolean hasTwoPair(Card[] cards)

public static boolean hasThreeOfAKind(Card[] cards)

public static boolean hasStraight(Card[] cards)

public static boolean hasFlush(Card[] cards)

public static boolean hasFullHouse(Card[] cards)

public static boolean hasFourOfAKind(Card[] cards)

public static boolean hasStraightFlush(Card[] cards)

The parameter for each of these methods will be an array of exactly 5 cards. Each method will return true or false, based on whether or not the given set of cards satisfies the poker hand being evaluated in the method. For example, the hasTwoPair method will return true if the set of cards has two pairs of different values (e.g. a pair of 4's and a pair of Jacks). If you need to review the various "poker hands" being tested in the methods, please take another look at the Poker Hand Page.

Important: Each of these methods checks whether or not the set of cards satisfies the given poker hand, but it does not care if it could also satisfy a better hand. For example, if the current hand is

<Ace of diamonds, Ace of spades, Ace of hearts, Jack of Spades, Jack of Diamonds>

then the results of calling each method should be as indicated below:

hasPair -- true (there is at least one pair)

hasTwoPair -- true (there are two pairs of distinct values)

hasThreeOfAKind -- true (there are three Aces)

hasStraight -- false

hasFlush -- false

hasFullHouse -- true (there are three Aces and two Jacks -- that makes a "full house")

hasFourOfAKind -- false

hasStraightFlush -- false

Important: In order for a hand to qualify as "Two Pair", it must have two pairs of distinct values -- in particular, having four-of-a-kind does not also count as two pair. For example, if the current hand is

<2 of diamonds, 2 of spades, 2 of hearts, 2 of clubs, 9 of spades>

then the results of calling each method should be as indicated below:

hasPair -- true (there is at least one pair)

hasTwoPair -- false (there are not two pairs of distinct values)

hasThreeOfAKind -- true (there are three 2's)

hasStraight -- false

hasFlush -- false

hasFullHouse -- false

hasFourOfAKind -- true (there are four 2's)

hasStraightFlush -- false

Explanation / Answer

public static boolean hasTwoPair(Card[] cards)
{
public static final int IS_TWO_PAIRS      = 2000000;
int val = 0;

      sortCardRank(cards);

      if ( cards[0].rank() == cards[1].rank() &&
           cards[2].rank() == cards[3].rank() )
         val = 14*14*cards[2].rank() + 14*cards[0].rank() + cards[4].rank();
      else if ( cards[0].rank() == cards[1].rank() &&
                cards[3].rank() == cards[4].rank() )
         val = 14*14*cards[3].rank() + 14*cards[0].rank() + cards[2].rank();
      else
         val = 14*14*cards[3].rank() + 14*cards[1].rank() + cards[0].rank();

      val= TWO_PAIRS + val;
    if(val==1)
   return true;
   else return false;

}


public static boolean hasStraight(Card[] cards)
{
public static final int IS_STRAIGHT       = 4000000;
int val=IS_STRAIGHT + HighestCard(cards);
if(val==1)
   return true;
   else return false;
}

public static boolean hasFlush(Card[] cards)
{
public static final int FLUSH          = 5000000;
int val= FLUSH + HighestCard(cards);
      if(val==1)
   return true;
   else return false;
}

public static boolean hasFullHouse(Card[] cards)
{

public static final int IS_FULL_HOUSE     = 6000000;
sortCardRank(cards);

      int val= IS_FULL_HOUSE + cards[2].rank();
      if(val==1)
   return true;
   else return false;
}


public static boolean hasFourOfAKind(Card[] cards)
{
public static final int IS_FOUR_OF_A_KIND = 7000000;
sortCardRank(cards);

      int val= IS_FOUR_OF_A_KIND + cards[2].rank();
      if(val==1)
   return true;
   else return false;

}

public static boolean hasStraightFlush(Card[] cards)
{
  int val;
public static final int HAS_STRAIGHT_FLUSH = 8000000;
      sortCardRank(cards);

      val = HAS_STRAIGHT_FLUSH+cards[0].rank() + 14* cards[1].rank() + 14*14* cards[2].rank()
            + 14*14*14* cards[3].rank() + 14*14*14*14* cards[4].rank();

   if(val==1)
   return true;
   else return false;
}

public static void sortCardRank( Card[] cards )
   {
      int i, j, mininum;

      for ( i = 0 ; i < cards.length ; i ++ )
      {
         mininum = i;   // Assume elem i (cards[i]) is the minimum

         for ( j = i+1 ; j < cards.length ; j++ )
         {
            if ( cards[j].rank() < cards[mininum].rank() )
            {
               mininum = j;    // We found a smaller minimum, update mininum    
            }
         }
         Card required = cards[i];
         cards[i] = cards[mininum];
         cards[mininum] = required;
      }
   }
  
    public static int HighestCard( Card[] cards )
   {
      int val;

      sortCardRank(cards);

      val = cards[0].rank() + 14* cards[1].rank() + 14*14* cards[2].rank()
            + 14*14*14* cards[3].rank() + 14*14*14*14* cards[4].rank();

      return val;
   }