Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

//Returns the number of pairs this hand contains public int numPairs() //Returns true if this hand has 3 cards that are of the same value

//Returns the number of pairs this hand contains public int numPairs() //Returns 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, null 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)

/** * An object of type Card represents a playing card from a * standard Poker deck, including Jokers. The card has a suit, which * can be spades, hearts, diamonds, clubs, or joker. A spade, heart, * diamond, or club has one of the 13 values: ace, 2, 3, 4, 5, 6, 7, * 8, 9, 10, jack, queen, or king. Note that "ace" is considered to be * the smallest value. A joker can also have an associated value; * this value can be anything and can be used to keep track of several * different jokers. */ public class Card { public final static int SPADES = 0; // Codes for the 4 suits, plus Joker. public final static int HEARTS = 1; public final static int DIAMONDS = 2; public final static int CLUBS = 3; public final static int JOKER = 4; public final static int ACE = 1; // Codes for the non-numeric cards. public final static int JACK = 11; // Cards 2 through 10 have their public final static int QUEEN = 12; // numerical values for their codes. public final static int KING = 13; /** * This card's suit, one of the constants SPADES, HEARTS, DIAMONDS, * CLUBS, or JOKER. The suit cannot be changed after the card is * constructed. */ private final int suit; /** * The card's value. For a normal card, this is one of the values * 1 through 13, with 1 representing ACE. For a JOKER, the value * can be anything. The value cannot be changed after the card * is constructed. */ private final int value; /** * Creates a Joker, with 1 as the associated value. (Note that * "new Card()" is equivalent to "new Card(1,Card.JOKER)".) */ public Card() { suit = JOKER; value = 1; } /** * Creates a card with a specified suit and value. * @param theValue the value of the new card. For a regular card (non-joker), * the value must be in the range 1 through 13, with 1 representing an Ace. * You can use the constants Card.ACE, Card.JACK, Card.QUEEN, and Card.KING. * For a Joker, the value can be anything. * @param theSuit the suit of the new card. This must be one of the values * Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER. * @throws IllegalArgumentException if the parameter values are not in the * permissible ranges */ public Card(int theValue, int theSuit) { if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS && theSuit != CLUBS && theSuit != JOKER) throw new IllegalArgumentException("Illegal playing card suit"); if (theSuit != JOKER && (theValue < 1 || theValue > 13)) throw new IllegalArgumentException("Illegal playing card value"); value = theValue; suit = theSuit; } /** * Returns the suit of this card. * @returns the suit, which is one of the constants Card.SPADES, * Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER */ public int getSuit() { return suit; } /** * Returns the value of this card. * @return the value, which is one of the numbers 1 through 13, inclusive for * a regular card, and which can be any value for a Joker. */ public int getValue() { return value; } /** * Returns a String representation of the card's suit. * @return one of the strings "Spades", "Hearts", "Diamonds", "Clubs" * or "Joker". */ public String getSuitAsString() { switch ( suit ) { case SPADES: return "Spades"; case HEARTS: return "Hearts"; case DIAMONDS: return "Diamonds"; case CLUBS: return "Clubs"; default: return "Joker"; } } /** * Returns a String representation of the card's value. * @return for a regular card, one of the strings "Ace", "2", * "3", ..., "10", "Jack", "Queen", or "King". For a Joker, the * string is always numerical. */ public String getValueAsString() { if (suit == JOKER) return "" + value; else { switch ( value ) { case 1: return "Ace"; case 2: return "2"; case 3: return "3"; case 4: return "4"; case 5: return "5"; case 6: return "6"; case 7: return "7"; case 8: return "8"; case 9: return "9"; case 10: return "10"; case 11: return "Jack"; case 12: return "Queen"; default: return "King"; } } } /** * Returns a string representation of this card, including both * its suit and its value (except that for a Joker with value 1, * the return value is just "Joker"). Sample return values * are: "Queen of Hearts", "10 of Diamonds", "Ace of Spades", * "Joker", "Joker #2" */ public String toString() { if (suit == JOKER) { if (value == 1) return "Joker"; else return "Joker #" + value; } else return getValueAsString() + " of " + getSuitAsString(); } public boolean equals(Object o){ if ( ! (o instanceof Card)) return false; Card c = (Card)o; if (c.suit == this.suit && c.value == this.value) return true; return false; } } // end class Card 

 /** * An object of type Hand represents a hand of cards. The * cards belong to the class Card. A hand is empty when it * is created, and any number of cards can be added to it. */ 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= 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 < 0 || 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 < hand.length; i++) { Card c1 = hand[i]; if (c1 != null){ if ( c1.getSuit() < c.getSuit() || (c1.getSuit() == c.getSuit() && c1.getValue() < c.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 < hand.length; i++) { Card c1 = hand[i]; if (c1 != null){ if ( c1.getValue() < c.getValue() || (c1.getValue() == c.getValue() && c1.getSuit() < c.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                        

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students explore these related Databases questions