Question: Please look at the following code to implement these instance methods in Hand.java: import java.util.ArrayList; public class Hand { private Card[] hand; // The cards
Please look at the following code to implement these instance methods in Hand.java:

import java.util.ArrayList;
public class Hand {
private Card[] hand; // The cards in the hand.
private int count;
/**
* Create a hand that is initially empty.
*/
public Hand() {
hand = new Card[5];
count = 0;
}
/**
* Remove all cards from the hand, leaving it empty.
*/
public void clear() {
for(int i=0 ; i count = 0; } /** * Add a card to the hand. It is added at the end of the current hand. * @param c the non-null card to be added. * @throws NullPointerException if the parameter c is null. */ public void addCard(Card c) { for(int i=0 ; i if (hand[i] == null){ hand[i] = c; count = count + 1; break; } } } /** * Remove a card from the hand, if present. * @param c the card to be removed. If c is null or if the card is not in * the hand, then nothing is done. */ public void removeCard(Card c) { for(int i=0 ; i if (hand[i].equals(c)){ hand[i] = null; count = count-1; } } } /** * Remove the card in a specified position from the hand. * @param position the position of the card that is to be removed, where * positions are starting from zero. * @throws IllegalArgumentException if the position does not exist in * the hand, that is if the position is less than 0 or greater than * or equal to the number of cards in the hand. */ public void removeCard(int position) { if (position = hand.length) throw new IllegalArgumentException("Position does not exist in hand: " + position); hand[position] = null; } /** * Returns the number of cards in the hand. */ public int getCardCount() { return count; } /** * Gets the card in a specified position in the hand. (Note that this card * is not removed from the hand!) * @param position the position of the card that is to be returned * @throws IllegalArgumentException if position does not exist in the hand */ public Card getCard(int position) { if (position = hand.length) throw new IllegalArgumentException("Position does not exist in hand: " + position); return hand[position]; } /** * Sorts the cards in the hand so that cards of the same suit are * grouped together, and within a suit the cards are sorted by value. * Note that aces are considered to have the lowest value, 1. */ public void sortBySuit() { int size = count; int nonnull = 0; int index = 0; Card[] newHand = new Card[5]; while (size > 0) { if (hand[nonnull] == null) { nonnull = nonnull+1; continue;} int pos = nonnull; // Position of minimal card. Card c = hand[nonnull]; // Minimal card. for (int i = nonnull+1; i Card c1 = hand[i]; if (c1 != null){ if ( c1.getSuit() (c1.getSuit() == c.getSuit() && c1.getValue() pos = i; c = c1; } } } hand[pos] = null; size = size - 1; newHand[index++] = c; nonnull = 0; } hand = newHand; } /** * Sorts the cards in the hand so that cards of the same value are * grouped together. Cards with the same value are sorted by suit. * Note that aces are considered to have the lowest value, 1. */ public void sortByValue() { int size = count; int nonnull = 0; int index = 0; Card[] newHand = new Card[5]; while (size > 0) { if (hand[nonnull] == null) { nonnull = nonnull+1; continue;} int pos = nonnull; // Position of minimal card. Card c = hand[nonnull]; // Minimal card. for (int i = nonnull+1; i Card c1 = hand[i]; if (c1 != null){ if ( c1.getValue() (c1.getValue() == c.getValue() && c1.getSuit() pos = i; c = c1; } } } hand[pos] = null; size = size - 1; newHand[index++] = c; nonnull = 0; } hand = newHand; } public void printHand(){ for(int i=0; i if (hand[i] != null){ System.out.println(hand[i]); } } System.out.println(); } /******************************** Implement your methods here ****************************************/ //Returns the number of pairs this hand contains public int numPairs() true if this hand has 3 cards that are of the same value public boolean hasTriplet() //Returns true if this hand has all cards that are of the same suit public boolean hasFlush() //Returns true if this hand has 5 consecutive cards of any suit public boolean hasStraight() //Returns true if this hand has a triplet and a pair of different //values public boolean hasFullHouse() //Returns true if this hand has 4 cards that are of the same value public boolean hasFourOfAKind() //Returns the card with the highest value in the hand. When there is //more than one highest value card, you may return any one of them public Card highestValue() //Returns the highest valued Card of any pair or triplet found, -1 if // none. When values are equal, you may return either public Card highestDuplicate() //Returns -1 if the instance hand loses to the parameter hand, 0 if //the hands are equal, and +1 if the instance hand wins over the //parameter hand. Hint: you might want to use some of the methods //above public int compareTo(Hand h) }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
