Question: Part 2: Playing Cards using Selection Statements Objective: The purpose of this exercise is to give you practice using if/selection statements and the Math



Part 2: Playing Cards using Selection Statements Objective: The purpose of this exercise is to give you practice using if/selection statements and the Math class. 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. Write your java code in a program called card.java 2. 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 3. 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 (suitName = "clubs"; for example) 4. 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. 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 5. Use if statements to assign the suit name. 6. Add one to cardNumber, and use the modulus operation to determine the card identity as follows: ((cardNumber+1) % 13) 1. 0 = King 2. 1 Ace = 3. 2-10 is the identity 4. 11 = Jack 5. 12 = Queen 7. Use a switch statement to assign the card identity. 8. Print out the name of the randomly selected card. 9. Make sure your code is properly commented and you use good style (camelCase, proper indentation, etc). Examples of Random Numbers: Random Number = 51 (CardNumber) 51/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
Step by Step Solution
3.42 Rating (149 Votes )
There are 3 Steps involved in it
public class Card public static void mainString args Step 2 Generate a random number between 0 and 5... View full answer
Get step-by-step solutions from verified subject matter experts
