Introduction to Programming Introduction h of the h the rds progr nd h developed
ID: 3840333 • Letter: I
Question
Introduction to Programming Introduction h of the h the rds progr nd h developed it ltiple for by refactoring the of the progr ultiple cla Card, D Poker Deck of Cards, shuffle the Deck, and th ut Cards to ultiple Hand P. Pi pecified fo classes, bmit the four Java source code files Card 2. Deck Hand Poker-java to the B board As class Card 20 represent nk, and ays of stri 41 Defi ng el med SUITS nd RANKS. Ther of the progra In the previous versi rrays detin dditional symbolic SUITS, NRANKS, and NCARDS (the st being the product of the first tw 31 There will be ene instance field named cardNum of type int. Dedare 3]Det insta field. single constructor at accepts Define methods to the ones previously written in he older version of the prog int ank int suit ethod St g tostring a string of the form: 21 Y y write thod that you class Deck [30 This class represen a deck of cards l we assume a normal card de hout any jokersM maintains an array of cards and keeps being dealt. It provid rds, shuffling the deck, and producing a string version of the deck for display put rposes (most likely for debugeine) 4] Import cla ded by til. Random ut c. java util -NosuchElementException me, Card import the prefix the Card NCARD5 be referred by just NCARDS import tatic exam 3 .Card 31 De n 3 insti ce fields: Card deck the array to hold the he de tc dthe inde Random and ber g ed by shuttle (thi field ly need that may be invoked ittpletl ate the deck rray with NCARDS el 21 set h the Card hose cardNu its index the deck. start dealing the top of the d 1] Instantiate berger id uffl [21 This od should be as the a we developed earlie ethod void swap int i int j 12) ght wish to instance field d k so tha needs to be specified in the pet eter list 6) Define the hod String to String [5) This NRANKS of NSUIT ng that ide (left fied followed by the length of the longest tring. minate the string pace after the display of thi 4] Define the method Card deal Check if the deck next Card NCARDS throw empty the NoSI hEl tE String eof tCard pti not empty then ret next Card the deck and he next Card field 7 [2] Y may wri at you class Hand [20] The Hand class is used to hold a hi nd of sol umber of Cal 1] Defi symbolic tant CARDS PER HAND Card hand d the cards int nCard ently 3. Deri tantiate the hand array with CARDS PER HAND pty h 5] Define thi hod void d Ci he Card in the dex nCards to the paramet ring to String 5] Defi e the method S Use Arrays tostring(this and to b class Poker 130] he main class for the program (game) stant NHANDS to have th Detine the symbolic Deck s, named hand, havine NHANDS rray of Hand ele nt of d object Each will initially b 2] Pr rint out the huffled deck ted loops. erate Hand, CARDS PER HAND times using the loop control variable kard. 3) The outer loop wil tee NHANDS ti able hand. ng the loop 4) Using de k.d the hand sp fied by the in ocp control h of the hands that up in step 3, above Ry the System out.printf hand and %d hand javal to the Assignment/Exam3 fold Test out your progr nd upload e small main e Card and Deck classes to uni You may find elpful to methods for est your class definitions. When Poker class, y its main es' main theExplanation / Answer
public class Card
{
private int suit, cardNumber;
private String[] SUITS = {"Clubs", "Spades", "Diamonds", "Hearts"};
private String[] RANKS = {"Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
public static int NSUITS = 4;
public static int NRANKS = 13;
public static int NCARDS = 52;
public Card(int rank,int suit) {
this.cardNumber = rank;
this.suit = suit;
}
public String toString() {
String finalCard = RANKS[cardNumber] + " of " + SUITS[suit];
return finalCard;
}
public int getRank(){
return cardNumber;
}
public int getSuit(){
return suit;
}
public String getRankString(){
return RANKS[cardNumber];
}
public String getSuitString(){
return SUITS[suit];
}
}
import java.util.Collections;
import java.util.ArrayList;
public class Deck
{
private ArrayList<Card> deck = new ArrayList<>(Card.NCARDS);
public Deck()
{
for(int a =0; a<Card.NSUITS; a++)
{
for(int b =0; b<Card.NRANKS; b++)
{
deck.add(new Card(b, a));
}
}
shuffle();
}
public Card deal(){
Card card = deck.get(0);
deck.remove(0);
return card;
}
public void shuffle(){
Collections.shuffle(deck);
}
public String toString()
{
String result = "Cards in Deck : " + deck;
return result;
}
}
import java.util.ArrayList;
public class Hand {
public static int CARDS_PER_HANDS = 5;
private ArrayList<Card> hand;
private int nCards;
public Hand(){
hand = new ArrayList<>(CARDS_PER_HANDS);
nCards =0;
}
public int getnCards() {
return nCards;
}
public void addCard(Card card){
hand.add(card);
nCards++;
}
public void setnCards(int nCards) {
this.nCards = nCards;
}
public String toString(){
String result = "Cards in hand : " + hand;
return result;
}
}
public class Poker {
public static void main(String[] args) {
int NHANDS = 4;
Deck deck = new Deck();
deck.shuffle();
System.out.println(deck);
Hand[] hands = new Hand[NHANDS];
//private ArrayList<Card> hands = new ArrayList<>(Card.NCARDS);
for(int i=0; i<Hand.CARDS_PER_HANDS; i++){
for(int hand=0; hand<NHANDS; hand++){
if(hands[hand]==null){
hands[hand] = new Hand();
}
hands[hand].addCard(deck.deal());
}
}
for(int hand=0; hand<NHANDS; hand++){
System.out.println(hands[hand]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.