Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This assignment requires that you write a program (name it DemoCard.java) that will create an array of cards, populate that array with instances of the

This assignment requires that you write a program (name it DemoCard.java) that will create an array of cards, populate that array with instances of the class card (a standard deck), shuffle that array, create two additional arrays of cards of size 5 and place 5 cards from the original shuffled array into each of the two new arrays, alternating between the two new arrays. The methods in the class DeckMethods provide all that you need to do this - use those methods. In plain English you are creating a deck of cards, shuffling that deck, and then dealing 5 cards to two different players. You are then to sort the two arrays that were dealt by the int stored in the instance variable value (you will have to write your own code for this - the methods in the Arrays class will not help you, and there is no Sort method in the DeckMethods class.

1 /* 2 * Author: Prof Brown-Sederberg 3 * Description: This class provides methods to manipulate object of type Card (Card.java) 4 * Date Last Revision: 6.15.2018 5 */ 6 7 8 public class DeckMethods 9 { 10 /* 11 * appends an instance of the class Card to th an array of instances of the class card 12 * if there is room in hat array 13 */ 14 public static void appendCard(Card[] deckOfCardsPassed, 15 Card cardToAppend) 16 { 17 deckOfCardsPassed[countElements(deckOfCardsPassed)] = cardToAppend; 18 }//end method appendCard 19 20 /* 21 * returns the number of elements in an array of Cards 22 * i.e. the number of spaces filled with instances of the class Card 23 * in the array 24 */ 25 public static int countElements(Card[] deckOfCardsPassed) 26 { 27 int count = 0; 28 for(int i = 0; i < deckOfCardsPassed.length; ++i) 29 { 30 if(deckOfCardsPassed[i] != null) 31 ++count; 32 }//end loop 33 return count; 34 }//end method countElements 35 36 /* 37 * returns thefirst card (in position [0] of an aray of Cards 38 * moves the remaining Cards forward in the array 39 */ 40 public static Card popCard(Card[] deckOfCardsPassed) 41 { 42 Card cardToBeReturned = deckOfCardsPassed[0]; 43 for(int i = 1; i < deckOfCardsPassed.length; ++i) 44 { 45 deckOfCardsPassed[i - 1] = deckOfCardsPassed[i]; 46 }//end for loop 47 48 deckOfCardsPassed[deckOfCardsPassed.length - 1] = null; 49 return cardToBeReturned; 50 }//end popCard 51 52 /* 53 * populates and array with nulls 54 */ 55 public static void populateNull(Object[] arrayPassed) 56 { 57 for(int i = 0; i < arrayPassed.length; ++i) 58 { 59 arrayPassed[i] = null; 60 }//end for loop 61 }//end method populateNull 62 63 /* 64 * Divides an array of instances of class Card into two arrays of equal size 65 */ 66 public static void cutTheDeck(Card[] deckOfCardsPassed, 67 Card[] playerDeckPassed, Card[]computerDeckPassed) 68 { 69 for(int i = 0; i < deckOfCardsPassed.length/2; ++i) 70 { 71 playerDeckPassed[i] = deckOfCardsPassed[i]; 72 computerDeckPassed[i] = 73 deckOfCardsPassed[i + deckOfCardsPassed.length/2]; 74 }//end for loop 75 }//end method cutTheDeck 76 77 /* 78 * Prints the values of the valiables of instances of the class Card 79 * in an array of Cards 80 */ 81 public static void printDeck(Card[] deckPassed) 82 { 83 for(int i = 0; i < deckPassed.length; ++i) 84 { 85 if(deckPassed[i] != null) 86 System.out.println(deckPassed[i].toString()); 87 }//end loop 88 }//end method printDeck 89 90 /* 91 * Randomizes the instances of the class Card within an 92 * array of instaces of the class Card 93 */ 94 public static void shuffleDeck(Card[] deckPassed) 95 { 96 for(int i = 0; i < deckPassed.length; ++i) 97 { 98 int j = (int)(Math.random() * deckPassed.length); 99 Card temp = deckPassed[i]; 100 deckPassed[i] = deckPassed[j]; 101 deckPassed[j] = temp; 102 deckPassed[i].setShuffled(true); 103 deckPassed[j].setShuffled(true); 104 }//end for loop 105 }//end method shuffleDeck 106 107 /* 108 * Creates instances of the class Card and populates an array of instances of the class Card 109 * with those instances, the array therefore holds 52 instances of the class Card representing an 110 * ushuffeled deck of cards 111 */ 112 public static void populateDeck(Card[] deckPassed) 113 { 114 int position = 0; 115 String[] suit = {"Hearts", "Clubs", "Diamonds", "Spades"}; 116 String[] face = {"Ace", "Two", "Three", "Four", "Five", "Six", 117 "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; 118 int[] value ={1,2,3,4,5,6,7,8,9,10,10,10,10}; 119 for(int i = 0; i < suit.length; ++i) 120 { 121 for(int j = 0; j < face.length; ++j) 122 { 123 deckPassed[position] = new Card(suit[i], face[j], value[j], 124 false, false); 125 position++; 126 }//end inner loop 127 }//end outer for loop 128 129 }//end method shuffleDeck 130 131 }//end class

1 /* 2 * Author: Prof Brown-Sederberg 3 * Description: This class represents a playing card 4 * Date Last Revision: 6.15.2018 5 */ 6 7 public class Card 8 { 9 //instance variables 10 private String suit; 11 private String face; 12 private int value; 13 private boolean shuffled; 14 private boolean dealt; 15 16 //constructors 17 public Card() 18 { 19 suit = ""; 20 face = ""; 21 value = 0; 22 shuffled = false; 23 dealt = false; 24 }//end default constructor 25 26 public Card(String suitPassed, String facePassed, 27 int valuePassed, boolean shuffledPassed, 28 boolean dealtPassed) 29 { 30 suit = suitPassed; 31 face = facePassed; 32 value = valuePassed; 33 shuffled = shuffledPassed; 34 dealt = dealtPassed; 35 }//end constructor method 36 37 //getters 38 public String getSuit() 39 { 40 return suit; 41 }//end getSuit 42 43 public String getFace() 44 { 45 return face; 46 }//end getFace 47 48 public int getValue() 49 { 50 return value; 51 }//end getValue 52 53 public boolean getShuffled() 54 { 55 return shuffled; 56 }//end getShuffled 57 58 public boolean getDealt() 59 { 60 return dealt; 61 }//end getDealt 62 63 //setter methods 64 public void setSuit(String suitPassed) 65 { 66 suit = suitPassed; 67 }//end setSuit 68 69 public void setFace(String facePassed) 70 { 71 face = facePassed; 72 }//end setFace 73 74 public void setValue(int valuePassed) 75 { 76 value = valuePassed; 77 }//end setValue 78 79 public void setShuffled(boolean shuffledPassed) 80 { 81 shuffled = shuffledPassed; 82 }//end setShuffled 83 84 public void setDealt(boolean dealtPassed) 85 { 86 dealt = dealtPassed; 87 }//end setDealt 88 89 public String toString() 90 { 91 return "Suit: " + suit + " Face: " + face + " Value: " + value + " Shuffled: " + shuffled + " Dealt: " + dealt; 92 } 93 94 }//end class card

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

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

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions