Question: Need this in Java just having issue getting the deck of cards to print out and show if a hand has a flush or not.
Need this in Java just having issue getting the deck of cards to print out and show if a hand has a flush or not. The base of the program is given by the instructor.
public class Card {
public static final String[] RANKS = { null, "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
public static final String[] SUITS = { "Clubs", "Diamonds", "Hearts", "Spades"};
private final int rank;
private final int suit;
/** * Constructs a card of the given rank and suit. */ public Card(int rank, int suit) { this.rank = rank; this.suit = suit; }
/** * Returns a negative integer if this card comes before * the given card, zero if the two cards are equal, or * a positive integer if this card comes after the card. */ public int compareTo(Card that) { if (this.suit < that.suit) { return -1; } if (this.suit > that.suit) { return 1; } if (this.rank < that.rank) { return -1; } if (this.rank > that.rank) { return 1; } return 0; }
/** * Returns true if the given card has the same * rank AND same suit; otherwise returns false. */ public boolean equals(Card that) { return this.rank == that.rank && this.suit == that.suit; }
/** * Gets the card's rank. */ public int getRank() { return this.rank; }
/** * Gets the card's suit. */ public int getSuit() { return this.suit; }
/** * Returns the card's index in a sorted deck of 52 cards. */ public int position() { return this.suit * 13 + this.rank - 1; }
/** * Returns a string representation of the card. */ public String toString() { return RANKS[this.rank] + " of " + SUITS[this.suit]; }
}
The code for this chapter is in the ch12 directory of ThinkJavaCode2. See page xviii for instructions on how to download the repository. Before you start the exercises, we recommend that you compile and run the examples.
Exercise 12.1 Encapsulate the deck-building code from Section 12.6 in a method called makeDeck that takes no parameters and returns a fully- populated array of Cards.
Exercise 12.3 In Poker a \ ush" is a hand that contains ve or more cards of the same suit. A hand can contain any number of cards. 1. Write a method called suitHist that takes an array of cards as a pa- rameter and that returns a histogram of the suits in the hand. Your solution should only traverse the array once as in Section 7.7. 2. Write a method called hasFlush that takes an array of cards as a param- eter and returns true if the hand contains a ush (and false otherwise).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
