Write a class named PokerHand that will represent a set of 5 playing cards store
ID: 3640090 • Letter: W
Question
Write a class named PokerHand that will represent a set of 5 playing cards stored as an array of integers (cards are represented as the numbers 1 through 52). In addition to a constructor, your class should contain the following methods:public boolean haveCard(int card)
- will return true if the card is in the hand
public void displayCards( )
- will display the 5 cards (display as integers is OK)
public void dealCards( )
- will populate the array with a random set of cards
- the cards must be unique(i.e., no duplicate cards)
- hint: use the haveCard method to determine if the card is unique
Explanation / Answer
01 public class PokerHand 02 { 03 // instance variables - replace the example below with your own 04 private int [] hand; 05 private Random generator; 06 07 /** 08 * Constructor for objects of class PokerHand. 09 */ 10 public PokerHand() 11 { 12 // initialise instance variables 13 this.hand = new hand[5]; 14 } 15 16 /** 17 * The haveCard method will return true if the card is in the hand. 18 * 19 * @param card a sample parameter for a method 20 * @return boolean value 21 */ 22 public boolean haveCard(int card) 23 { 24 //determine if the card is unique 25 if (play[] == card) 26 { 27 return true; 28 } 29 return false; 30 } 31 32 /** 33 * The displayCards method will display the 5 cards as integers. 34 */ 35 public void displayCards() 36 { 37 System.out.println(hand); 38 39 } 40 41 /** 42 * The dealCards method will generate a random set of cards and 43 * determine if the card is unique. 44 * 45 */ 46 public void dealCards() 47 { 48 Random generator = new Random(); 49 50 51 for (int i = 0, i < 5, i++) 52 { 53 if (hand.haveCard == false) 54 { 55 //generate 5 random cards as integers 56 play[i] = 1 + generator.nextInt(52); 57 } 58 59 } 60 }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.