Question

JAVA JAVA 1- Create two arrays one for the rank of the playing cards and one...

JAVA JAVA

1- Create two arrays one for the rank of the playing cards and one for the suit.

2- Create an array of 52 strings that contain a deck of playing cards. The first 13 items are the cards, whose suit is clubs, the next 13 diamonds, the next 13 hearts and finally the spades. In this step, the array and the order of the cards should be printed.

3- Use the shuffling algorithm you have learnt in the lecture and shuffle the cards and print the result.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

Program

import java.util.Random;

/**
* Program to generate shuffling algorithm in string array
* Create a rank array and suit array
* then combine this array to generate string array of 52 elements
* @author deept
*
*/
public class ShufflingAlgorithm {

   public static void main(String[] args) {
       //Create two arrays one for the rank of the playing cards and one for the suit.
       String suits[]= {"clubs", "diamonds", "hearts", "spades"};
       String ranks[]= {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
       //Create an array of 52 strings that contain a deck of playing cards
       String deck[]=new String[52];
       int k=0,i=0;
       while(i<52) {
           for(int j=0;j<13;j++) {
               deck[i]=ranks[j];
               deck[i]+=" "+suits[k];
               i++;
           }
           k++;
       }
       //Use the shuffling algorithm,shuffle the cards and print the result.
       shuffleAlgorithm(deck);
       print(deck);
   }
/*
* Method to generate an array to shuffle
* @param Array to shuffle
* @Return None
*/
public static void shuffleAlgorithm(String[] deck) {
   Random rand = new Random();      
   for (int i=0; i<deck.length; i++) {
        int index = rand.nextInt(deck.length);
        String temp = deck[i];
        deck[i] = deck[index];
        deck[index] = temp;
   }
}
/*
* Method to print array
* @param string array to print deck array
* @Return nine
*/
public static void print(String[] deck) {
   for (int i=0; i<deck.length; i++) {
       System.out.println(deck[i]);
   }
}
}

-------------------------------------------------

Output

2 diamonds
2 spades
10 clubs
King hearts
3 clubs
10 spades
9 hearts
Ace diamonds
3 diamonds
10 hearts
7 spades
10 diamonds
Ace spades
King diamonds
5 clubs
King clubs
5 hearts
2 hearts
2 clubs
6 spades
Jack diamonds
5 spades
Jack clubs
8 diamonds
7 hearts
Jack hearts
Queen hearts
Queen clubs
Queen spades
8 spades
4 spades
Jack spades
6 diamonds
8 clubs
3 hearts
3 spades
Ace clubs
4 clubs
8 hearts
7 diamonds
4 diamonds
6 clubs
4 hearts
King spades
9 diamonds
Queen diamonds
5 diamonds
6 hearts
9 clubs
7 clubs
Ace hearts
9 spades

Add a comment
Know the answer?
Add Answer to:
JAVA JAVA 1- Create two arrays one for the rank of the playing cards and one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification...

    C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification file) Card.cpp (class implementation file) DeckOfCards.h (class specification file) DeckOfCards.cpp (class implementation file) 200_assign6.cpp (application program) NU eelLS Seven UT Diamonds Nine of Hearts Six of Diamonds For your sixth programming assignment you will be writing a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and an application program. Class Card should provide: a....

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! !!!!!!!!!!!!!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!!!!!!!!!!!!!!!!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in new deck order. (This is the order of a deck of cards new from the box.) The 2 Array lists...

  • An ordinary deck of playing cards has 52 cards. There are four suitslong dash​spades, ​hearts, diamonds,...

    An ordinary deck of playing cards has 52 cards. There are four suitslong dash​spades, ​hearts, diamonds, and clubslong dashwith 13 cards in each suit. Spades and clubs are​ black; hearts and diamonds are red. One of these cards is selected at random. Let A denote the event that a red card is chosen. Find the probability that a red card is​ chosen, and express your answer in probability notation. The probability that a red card is chosen is _____=______

  • CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...

    CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties an integer for the rank (1 (corresponding to Ace) ,2,3, 13 (correspond ing to King) and suit (Spades, Hearts, Diamonds, or Clubs). Make the suit an enumerated data type. Include getters and setters and a method that tests if a card is valid. Write a class named Deck whose instances are full...

  • bblem deals with playing cards. The Card API is given below: public class Card ( suit...

    bblem deals with playing cards. The Card API is given below: public class Card ( suit is "Clubs", "Diamonds", "Bearts", or "Spades" Gene=ination s 2", , "10" יי", ,"פ" ,"8" ,"ר" , "6" ,"5י ,-4" ,"ני- * or "A * value is the value of the card number if the card denominat, *is between 2 and 10; 11 for J, 12 for Q, 13 for K, 14 for A public Card (String suit, string denomination){} 1/returns the suit (Clubs, Diamonds,...

  • Example set of 52 poker playing cards Suit Ace 2 10 Jack Queen King 3- What...

    Example set of 52 poker playing cards Suit Ace 2 10 Jack Queen King 3- What is the sample space for a regular 52 card-deck? Spades Hearts 4-Determine the sample space Diamonds for choosing one of the four possible aces from a standard deck of cards. clubs

  • urgent help with python. can somone code this please and show output QUESTION: There are a variety of games possi...

    urgent help with python. can somone code this please and show output QUESTION: There are a variety of games possible with a deck of cards. A deck of cards has four different suits, i.e. spades (*), clubs (*), diamonds (), hearts (), and thirteen values for each suit. Now write a function that uses list comprehension to create a deck of cards. Each element in the list will be a card, which is represented by a list containing the suit...

  • 4 cards are randomly drawn from a standard deck of playing cards. What is the prob-...

    4 cards are randomly drawn from a standard deck of playing cards. What is the prob- ability that all their suits are different? Hint: There are 52 cards in a standard deck of playing cards. A card can have 4 different suits: diamond ( ♦ ), club ( ♣ ), heart ( ♥ ), or spades ( ♠ ). There are 13 cards of each suit. Cards are further labeled by their rank: numbers 1 to 10 and three face...

  • Use inheritance and classes to represent a deck of playing cards. Create a Cardclass that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriat...

    Use inheritance and classes to represent a deck of playing cards. Create a Cardclass that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriate accessors, constructors, and mutators. Next, create a Deck class that stores a vector of Card objects. The default constructor should create objects that represent the standard 52 cards and store them in the vector. The Deck class should have functions to: • Print every card in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT