Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

* Card objects from Lab 3. import org.omg.CORBA.TCKind; public class Card { private CardKind cKind; private CardSuit cSuit; public enum CardKind { TWO, THREE, FOUR,

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

* Card objects from Lab 3.

import org.omg.CORBA.TCKind; public class Card { private CardKind cKind; private CardSuit cSuit; public enum CardKind { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE } public enum CardSuit { SPADES, HEARTS, CLUBS, DIAMONDS } public Card (CardSuit cSuit, CardKind cKind ) { this.cKind = cKind; this.cSuit= cSuit; } public CardKind getCardKind() { return cKind; } public CardSuit getCardSuit() { return cSuit; } public String toString() { CardKind cKind = getCardKind(); String kind; if (cKind == CardKind.TWO) { kind = "2"; } else if (cKind == CardKind.THREE) { kind = "3"; } else if (cKind == CardKind.FOUR) { kind = "4"; } else if (cKind == CardKind.FIVE) { kind = "5"; } else if (cKind == CardKind.SIX) { kind = "6"; } else if (cKind == CardKind.SEVEN) { kind = "7"; } else if (cKind == CardKind.EIGHT) { kind = "8"; } else if (cKind == CardKind.NINE) { kind = "9"; } else if (cKind == CardKind.TEN) { kind = "10"; } else if (cKind == CardKind.JACK) { kind = "JACK"; } else if (cKind == CardKind.QUEEN) { kind = "QUEEN"; } else if (cKind == CardKind.ACE) { kind = "ACE"; } else if (cKind == CardKind.KING) { kind = "KING"; } return cKind + " 0f " + cSuit; } public static void main (String[] args) { Card card1 = new Card (CardSuit.HEARTS, CardKind.QUEEN); System.out.println(card1); } } 
Overview In this homework, you will write a simple method that classifies poker hands given an array of Card objects from Lab 3. Writing the Poker Hand class Create a new class in the same project as your Lab 3 Card.java file called Poker Hand.java. Write a class named Poker Hand in this file according to these rules: 1. Create a public enum inside Poker Hand named HandType. It must have 10 members representing the 10 types of poker hands, named HIGH_CARD, PAIR, THREE_OF_KIND, FOUR_OF_KIND, TWO_PAIR, FLUSH, FULL_HOUSE, STRAIGHT, STRAIGHT_FLUSH, and ROYAL_FLUSH. The enum members must be arranged in increasing order, such that the worst hand type is first in the enum and the best hand type is last. Here is a picture: 1 Royal Flush 6 Straight AD890 000 89012 2 Straight Flush 7 Three of a kind 3 Four of a kind 8 Two Pair 4 Full House 9 One Pair 5 Flush 10 High Card 2. Write a method public static HandType classifyHand(Card [] cards). This method takes an array of 5 cards which you can assume is in increasing order of card kind, e.g., all 2's come first, then 3's, etc. It determines which hand type the array represents, from the poker hand types above, and returns the appropriate HandType enum value for the hand. Determining a hand's type: Determining a poker hand's type is fairly simple (assuming you know what the different types of hands are) if the cards in the hand are sorted first. (You can assume that is the case for this assignment.) It should then be simple enough to count how many pairs, threes, and fours you have, and if you have a flush or straight. If you are clever, you will note that some hand types are mutu ally exclusive with others can you have both a pair and a straight?) and structure your code to take advantage of that. Validating Your Code To test your code: 1. Add a main method to your PokerHand class. 2. In your main, construct 10 different arrays of 5 card objects, one array for each of the possible hand types. (The cards must be in increasing order.) For each array: (a) Call your classifyHand method, passing the array as a parameter (b) Save the return value in a parameter of type HandType. (c) Compare the return value against what you know the return value should be, and if they are not equal, print an error message. (d) Run your program when you think you are done. Make sure that you don't get any error messages in output. Rejoice! Overview In this homework, you will write a simple method that classifies poker hands given an array of Card objects from Lab 3. Writing the Poker Hand class Create a new class in the same project as your Lab 3 Card.java file called Poker Hand.java. Write a class named Poker Hand in this file according to these rules: 1. Create a public enum inside Poker Hand named HandType. It must have 10 members representing the 10 types of poker hands, named HIGH_CARD, PAIR, THREE_OF_KIND, FOUR_OF_KIND, TWO_PAIR, FLUSH, FULL_HOUSE, STRAIGHT, STRAIGHT_FLUSH, and ROYAL_FLUSH. The enum members must be arranged in increasing order, such that the worst hand type is first in the enum and the best hand type is last. Here is a picture: 1 Royal Flush 6 Straight AD890 000 89012 2 Straight Flush 7 Three of a kind 3 Four of a kind 8 Two Pair 4 Full House 9 One Pair 5 Flush 10 High Card 2. Write a method public static HandType classifyHand(Card [] cards). This method takes an array of 5 cards which you can assume is in increasing order of card kind, e.g., all 2's come first, then 3's, etc. It determines which hand type the array represents, from the poker hand types above, and returns the appropriate HandType enum value for the hand. Determining a hand's type: Determining a poker hand's type is fairly simple (assuming you know what the different types of hands are) if the cards in the hand are sorted first. (You can assume that is the case for this assignment.) It should then be simple enough to count how many pairs, threes, and fours you have, and if you have a flush or straight. If you are clever, you will note that some hand types are mutu ally exclusive with others can you have both a pair and a straight?) and structure your code to take advantage of that. Validating Your Code To test your code: 1. Add a main method to your PokerHand class. 2. In your main, construct 10 different arrays of 5 card objects, one array for each of the possible hand types. (The cards must be in increasing order.) For each array: (a) Call your classifyHand method, passing the array as a parameter (b) Save the return value in a parameter of type HandType. (c) Compare the return value against what you know the return value should be, and if they are not equal, print an error message. (d) Run your program when you think you are done. Make sure that you don't get any error messages in output. Rejoice

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions