Java Programmin through eclipse. (Intro to computer programming class). Write a
ID: 3872522 • Letter: J
Question
Java Programmin through eclipse. (Intro to computer programming class). Write a program that simulates the action of picking a card from a deck of 52 cards. Your program should display the suit and rank of the card. Here are sample runs:
The card is 10 of Spades
The card is Jack of Clubs
Consider that each card could be represented by a number between 0 and 51
Suit:
Card numbers: 0 to 12 are Spades, 13 to 25 Hearts, 26 to 38 Diamonds, and 39 to 51represent Clubs.
Rank:
1 is the Ace
2 through 10 are as you would expect the2 through 10
11 through 13 are the Jack, Queen, and King respectively.
So,obtain a random number (cardNumber)from 0to 51and display its rank and suitusing the following:
Rank: (cardNumber %13) + 1, then use a multi-way if to determine the proper output.
Use a multi-way if to determine the output for the suit, based on the ranges listed above. You could, alternatively, useinteger division and dividecardNumberby 13, getting a number 0-3 that would be used to determine suit.
Explanation / Answer
PROGRAM CODE:
package cards;
import java.util.Random;
public class RandomDeckOfCards {
public static void main(String[] args) {
//strings representing the suits and ranks
String suits[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
String ranks[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
//Setting up a random number generator
Random rand = new Random();
//getting the random number
int number = rand.nextInt(52);
//System.out.println(number);
//calculating the card
String card = ranks[number%13] + " of " + suits[number/13];
//printing on screen
System.out.println("The card is " + card);
}
}
OUTPUT:
The card is 9 of Hearts
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.