Asked by ConstableCaribou2084
lab 2 information . Submission: CardGame. java...
Image transcription text
Submission: CardGame. java Assignment: In a typical card game, each player gets a hand of cards. The deck is shuffled, and cards are dealt one at a time from the deck and added to the players' hands. In some games, cards can be removed from a hand, and new cards can be added. The game is won or lost depending on the value (Ace, 2, 3, ..., King) of the cards that a player receives. Our card game will deal two hands of 5 cards per hand. Your program wfll determine who has the highest scoring hand. The starting code provided: 0 includes five arrays: 0 Two arrays with values assigned and three empty arrays. 0 requires you to write algorithms to o Create a deck of cards 0 Shuffle a deck of cards 0 Create a hand from a deck of cards 0 Determine who is the winner in a game of cards. In this program, called CardGame, you will create a deck of 52 cards, similar to what we did in Lab 2, this time represented by the arrays suitNames and rankNames. You will print out all the cards in the deck, shuffle the whole deck of cards (the elements in the array), print out the shuffled cards in the deck, create a hand of cards and print each hand. You will deal two hands of cards and determine who is the winner of the card game. 1. To create the deck of cards, use the suitNames and rankNames arrays to initialize each String element in the 52-element array to a sequence of two characters, each representing suit and then rank. 0 For full credit, do NOT initialize each element with 52 different statements. 0 Be sure to print the shuffled and unshuffled deck. 0 Clarification: Cards 1-13 represent the clubs, 14-26 represent the hearts, then spades, then diamonds. In all suits, card identities ascend in step with the card number, with Ace being high: 13 is the 2 of hearts, 14 is the 3 of hearts, and 25 is the Ace of hearts. - deck [ 0] (before shuffling) represents the 2 of Clubs ("C2") - deck [14] would be the 3 of Hearts ("H3") - deck [25] would be the Ace of Hearts ("HA")
Image transcription text
o Hint: use a nested for loop where suitNames controls the outer loop and rankNames controls the inner loop. Append the card abbreviations (3 characters max each) in the array called deck. 2. To shuffle a deck of cards the elements in the arrays must continuously randomly generate an index number of the list and swap the element at that index with the first element (at index 0). Swap how many times you like, but choosing a number greater than ~50 would be good as the deck holds 52 elements. o Use a for loop to shuffle the deck at least 52 times. Declare an instance of the Random class to generate a random number between 0-51. Ex: Random ran = new Random(); Don't forget to import java.util.Random; Note that using Math. random ( ) will not earn you full credit for this portion DO NOT USE ANY PREDEFINED METHODS TO SHUFFLE CARDS. THIS MUST BE YOUR OWN ALGORITHM. YOU MUST ADAPT THE ALGORITHM AS DESCRIBED ABOVE. 3. From the shuffled deck array, you should deal out 5 cards to each player's hand (represented by arrays, handi and hand2). o Cards that were taken from the deck to put in the hand array must never be used again. You can do this by keeping track of what your last index value is when dealing from the card deck. Always make sure you have cards left in the deck of cards when creating a hand. Again, keep track of your index value to see if you have enough cards in your deck to deal 2 hands. note: the game should prompt the user to continue playing as long as there are cards in the deck (in accordance with the output below)
Image transcription text
4. To determine who won the card game, add each card value together in hand1 to create a total handl value, then use the same algorithm to determine the total handz value. The hand with the highest score wins the game. 0 Create two variables to hold the total face value of each hand, and be sure to print out the hand values and indicate the winning hand for the user. 0 Ifinm: - first determine the card value by looking at the second character in the card. (You can use . CharAt (1) to isolate the card value from the second character). - See the chart below for the value of the second character in the card. Keep in mind that the second character of the card is a character, you must cast to an int. Second Character In Card value Below is starting code for the main program CardGameI import java.util.Scanner; public class CardGame{ public static void main(String args) { Scanner scan = new Scanner(System.in); //suits club, heart, spade or diamond
Image transcription text
String suitNames={ "C", "H", "S", "D") ; String rankNames={"2", "3", "4", "5". "6" , 171, "8", WOW "10", "J", "Q", "K", "A" }; String deck = new String [52] ; String handl = new String [5] ; String hand2 = new String [5] ;} } Sample Output: Unshuffled deck of cards C2 C3 C4 05 C6 C7 C8 09 C10 CJ CQ CK CA H2 H3 H4 H5 H6 H7 H8 H9 H10 HJ HQ HK HA S2 S3 S4 5 86 S7 S8 $9 S10 SJ SQ SK SA D2 D3 D4 D5 D6 D7 D8 D9 D10 DJ DQ DK DA Shuffled deck of cards (many different answers) S8 C3 D5 H10 $9 H4 H6 CQ C7 CJ S10 SQ SK H2 H3 CK HQ H5 D10 D8 H9 C5 D9 C6 HK HA H8 S3 HJ $4 $6 SA C8 D4 H7 SJ D3 09 $2 D2 85 $7 C2 D6 D7 C4 CA DK DJ DQ C10 DA Hand1 $8 C3 D5 H10 $9 Hand2 H4 H6 CQ C7 CJ Handl value is 35 Hand2 value is 37 Hand2 is the winner! Do you want to play again? (Y/N) No Goodbye
lab 2 information
Image transcription text
Scenario: You are a magician and you are practicing your card tricks. Since you don't want to reveal your secrets, you write a program that will pick a random card from the deck so you can practice your tricks alone. Use a random number generator to select a number from 0 to 51 (inclusive). Each number represents one card, and the suits are grouped: Cards 0-12 represent the diamonds, 13-25 represent the clubs, then hearts, then spades. In all suits, card identities ascend in step with the card number: 13 is the ace of clubs, 14 is the 2 of clubs, and 25 is the king of clubs. Follow the steps below to write your java program. 1. 2. $3.0" 9"?" Write your java code in a program called Card. java Generate a random number cardNumber using Java Math class. Note that we do not need to import the Math class like we do with Scanner. (See note below (Recall) step 6, to see how to use the random class for this assignment) 1. Note that you will generate a random number between 0 and 51 Declare two String variables: a String corresponding to the name of the suit and a String corresponding to the identity of the card (String suitName , cardIdentity ;). String is the Java type for text. String variables may be assigned any text and the text should be included between double quotations (sui tName = "clubs"; for example) Determine the suit name by using integer divide. (cardNumber / 13) 1. Cards 0-12 represent the diamond 2. 13-25 represent clubs, 3. 26-38 represents hearts 4. 39-51 represents spades Use if statements to assign the suit name. Add one to cardNumber, and use the modulus operation to determine the card identity as follows: ( (cardNumber+1) % 13) 1. O = King 2. 1 = Ace 3. 2 — 10 is the identity 4. 11 = Jack 5. 12 = Queen Use a switch statement to assign the card identity. Print out the name of the randomly selected card. Make sure your code is properly commented and you use good style (camelCase, proper indentation, etc).
Image transcription text
Examples of Random Numbers: Random Number = 51 (CardNumber) 51 l 13 = 4 Suit is Spade (SuitName) 52 % 13 = 0 Identity is King (Card Identity) King of Spades Random number 24 (CardNumber) 24/ 13 = 1 Suit is Clubs (SuitName) 25 % 13 = 12 Identity is Queen (Card Identity) Queen of Clubs Here are four sample runs of the program. (Your program need only generate one card for a single run.) You picked the 6 of Clubs You picked the Jack of Hearts You picked the Ace of Spades You picked the 4 of Diamonds Important note: this lab can be completed multiple ways: using lots of if statements, using multiple switch statements, using the modulus operator, etc. It is crucial that you understand all the different ways the lab can be completed, because they are all related to the new material we have just learned in the class. So, to prepare for the exam, do the lab a few different ways. However, you need only submit ONE program to receive credit for this lab. Recall: 1. 9" Remember to include a header and comments in your code. 2. Read through your compfler errors before asking for help. 3. 4. When using a switch statement, all statements under a matching condition will SAVE AND COMPILE AS YOU GO! run unless there is the keyword break. Switch statements have a default feature that serves a similar purpose to else in an if statement. Class Math has a method randomO that returns a random double in the range [0,1). Typically, we want a random number among some range of integers (or whole numbers). For example, we want to pick a random number from 2 to 13
Image transcription text
(including 13), so we can generate a random number from 0 to 11 and then add 2 as shown below. int number = (int)(Math.random() * 12) + 2; In general, use the following statement to generate a number from baseNum to upperBound inclusive. int number = (int)(Math.random()*(upperBound—baseNum+1))+baseNum; Since randomo returns a double, we use (int) to cast the double to an integer (truncating any decimals). We do this because we will use these integers for our switch statements. You will need to consider the range of numbers you want to randomly select from, given the problem statement above.
Answered by CountWren4040
Unlock full access to Course Hero
Explore over 16 million step-by-step answers from our library
Subscribe to view answerlisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, cons
- onec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit
- lestie consequat, ultrices ac magna. Fusce dui lect
- onec aliquet. Lorem ipsum dolor sit amet, consectetur a
- ipsum d
Answered by pranuthisreepadi
ec facilisis. Pellentesque dap
itur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet.Unlock full access to Course Hero
Explore over 16 million step-by-step answers from our library
Subscribe to view answerm risus ante, dapibus
gue
it
ec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante,
nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante,
m risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce
ac, dictum vitae odio. Donec aliquet. Lore
o. Don
Fusce dui lectus, con
gue
gue
ec facilisis. Pellentesque dapibus efficitur
gue
ng elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur la
s ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel
usce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum
sque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices
ng elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur
risus ante, dapibus a molestie consequat, ultrices ac
ctum vitae odio. Donec aliquet. Lo
ec facilisis.
llentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat,
ctum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing el
, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit
s ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel
a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem i
icitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui l
facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce d
ur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue v
ec facilisis.
ipsum dolor sit am
or nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibu
sque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices
acinia pulvinar tortor nec facilisis. Pellentesqu
dictum vitae odio. Donec aliquet.
amet, consectetur adipiscing elit. Nam lacinia pulvin
s a molestie consequat, ultrices ac magna. Fusce dui l
ur laoreet. Nam risus ante, dapibu
ec facilisis.
amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor ne
ec facilisis.
ctum vitae odio. Donec aliquet. Lo
sus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lect
ia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam ris
gue
ia pulvinar tortor nec f
gue
gue
ia pulvi
lestie consequat, ultrices ac magna. Fusce dui lectus, congue vel
ec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante
or nec facilisis. Pellentesque dapibus efficit
sus ante, d
sus ante, dapibus a molestie con
gue
s a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laore
ec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna.
sque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui le
ec facilisis.
m ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tor
gue
ce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum
trices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit
ongue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectet
gue
ur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fu
cing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a mol
entesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce
gue
cing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur lao
iscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus
nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie
ec facilisis.
ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur ad
Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulv
gue
ng elit. Nam lacinia pulvinar to
cing elit. Nam lacinia pulvinar tortor nec f
tesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie con
ec facilisis.
a. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet.
acinia pulvinar t
, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adip
acinia pulvinar t
dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. N
ur laoreet. Nam risus an
iscing elit. Nam lacinia pulvinar tortor nec facilisis. Pel
ur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac m
ia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoree
Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar
ec aliquet. Lorem ipsum dolor sit amet, consectetur adipisc
inia pulvinar tortor ne
or nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus
acinia pulvinar t
cing elit. Nam lacinia pulvinar tortor nec faci
m risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce du
acinia pulvinar t
sque dapibus efficitur laoreet. Nam risus ante, dapib
acinia pulvinar t
usce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet
ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pe
m ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor
Fusce dui lectus, congue vel laoreet ac, dictum vitae od
m ipsum dolor sit ame
entesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie conse
itur laoreet. Nam risus ante, dapibus a molestie
ctum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Na
e vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem
nec facilisis. Pellentesque da
onec aliquet. Lorem ipsum dolor sit
risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel lao
m risus ante, dapibus a molestie consequat, ultrices ac magna. Fusc
nec facilisis. Pellentesque da
m ipsum dolor sit ame
, ultrices ac magna. Fusce dui lectus, congue vel l
or nec facilisis. Pellentesque dapibus efficitur
ctum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Na
e vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem
molestie consequat, ultrices ac m
onec aliquet. Lorem ipsum dolor sit
risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel lao
m risus ante, dapibus a molestie consequat, ultrices ac magna. Fusc
nec facilisis. Pellentesque da
m ipsum dolor sit ame
acinia pulvinar t
trices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Done
s a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, d
icitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fu
fficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna.
s a molestie consequat, ultrices ac magna. Fusce dui
acinia pulvinar t
m ipsum dolor sit amet, consectetur adipisc
sus ante, dapibus a molestie consequat, u
nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ant
acinia pulvinar t
acinia pulvinar tortor nec facilisis
ec facilisis.
ctum vitae odio. Donec aliquet. Lo
inia pulvinar tortor nec facilisis. Pellentesque dapi
usce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec
ia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac mag
ctum vitae odio. Donec aliquet. Lo
ec facilisis.
ce dui lectus, congue vel laoreet ac
ec facilisis.
itur laoreet. Nam risus
gue
gue
ec facilisis. Pellente
gue
gue
gue