Question

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 can be looked at as a draw deck and a hand deck. The original array is used to populate the draw deck before dealing.

  • Write and use a method to build a deck of cards where the array of cards is available in Main.
  • Use heading or titles and make the output look nice.
  • Back in Main,  print out the cards, 4 per line, 1 suit per column. (using a loop might help)
  • Use the deck array to create an array list of cards and be able to shuffle those cards.
  • Print out the shuffled deck.
  • Deal out 5 cards to another array list and the remaining cards should be the draw pile. (you will be taking cards from one ArrayList and adding it to another.)
  • Print both the hand (5 cards) and the draw deck (47 cards).
  • Deal with 4 hands.

This is the output I am looking for:

Here is your sorted Deck! Ace of Spades, Ace of Hearts, Ace of Diamonds, Ace Of Clubs 2 of Spades, 2 of Hearts, 2 of Diamonds

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


ANSWER:-

CODE:-

import java.util.*;
class Card{
   String rank;
   String suit;
   Card(String rank, String suit){
       this.rank = rank;
       this.suit = suit;
   }
   public String toString(){
       return this.rank+" of "+this.suit;
   }
}
class Deck{
   private String[] suits = {"Spades","Hearts","Diamonds","Clubs"};
   private String[] ranks = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
   Card[] cards;
   Deck(){
       this.cards = new Card[52];
   }
   public Card[] createDeck(){
       int k = 0;
       for(int i=0;i<suits.length;i++){
           for(int j=0;j<ranks.length;j++){
               this.cards[k++] = new Card(ranks[j],suits[i]);
           }
       }
       return this.cards;
   }
   public ArrayList<Card> shuffle(Card[] cards){
       ArrayList<Card> list = new ArrayList<Card>();
       for(int i=0;i<cards.length;i++){
           list.add(cards[i]);
       }

       Random random = new Random();
       for(int i=list.size()-1;i>0;i--){
           int rand = random.nextInt(i+1);
           Card temp = list.get(rand);
           list.set(rand,list.get(i));
           list.set(i,temp);
       }
       return list;
   }
   public void dealCard(ArrayList<Card> deck,ArrayList<Card> hand){
       hand.add(deck.get(0));
       deck.remove(0);
   }
   public String printDeck(ArrayList<Card> cards){
       String str = "[";
       for(int i=0;i<cards.size();i++){
           String delimiter = (i==cards.size()-1)?"":", ";
           str += cards.get(i).toString()+delimiter;
       }
       str += "]";
       return str;
   }
   public void printCards(){
       System.out.println("\nHere is your sorted Deck!\n");
       for(int i=0;i<13;i++){
           int j=13;
           System.out.print(cards[i]+", ");
           System.out.print(cards[i+j]+", ");
           j += 13;
           System.out.print(cards[i+j]+", ");
           j += 13;
           System.out.println(cards[i+j]);
       }
       System.out.println();
   }
   public static void main(String[] args) {
       Deck deck = new Deck();
       deck.createDeck();
       deck.printCards();
       ArrayList<Card> shuffledDeck = deck.shuffle(deck.cards);
       System.out.print("The suffled deck is: ");
       System.out.println(deck.printDeck(shuffledDeck));
       ArrayList<Card> hand1 = new ArrayList<Card>();
       ArrayList<Card> hand2 = new ArrayList<Card>();
       ArrayList<Card> hand3 = new ArrayList<Card>();
       ArrayList<Card> hand4 = new ArrayList<Card>();
       ArrayList<Card> hand5 = new ArrayList<Card>();
       for(int i=0;i<5;i++){
           deck.dealCard(shuffledDeck,hand1);
           deck.dealCard(shuffledDeck,hand2);
           deck.dealCard(shuffledDeck,hand3);
           deck.dealCard(shuffledDeck,hand4);
           deck.dealCard(shuffledDeck,hand5);
       }
       System.out.print("\nThe cards for the first hand is: ");
       System.out.println(deck.printDeck(hand1));
       System.out.println();
       System.out.print("\nThe cards for the second hand is: ");
       System.out.println(deck.printDeck(hand2));
       System.out.println();
       System.out.print("\nThe cards for the third hand is: ");
       System.out.println(deck.printDeck(hand3));
       System.out.println();
       System.out.print("\nThe cards for the fourth hand is: ");
       System.out.println(deck.printDeck(hand4));
       System.out.println();
       System.out.print("\nThe cards for the fifth hand is: ");
       System.out.println(deck.printDeck(hand5));
       System.out.println();
       System.out.print("The draw pile is: ");
       System.out.println(deck.printDeck(shuffledDeck));
   }
}

NOTE:- If you need any modifications in the code,please comment below.Please give positive rating.THUMBS UP.

                THANK YOU!!!!

OUTPUT:-

Ace of Spades, Ace of Hearts, Ace of Diamonds, Ace of Clubs 2 of Spades, 2 of Hearts, 2 of Diamonds, 2 of Clubs 3 of Spades,

Add a comment
Know the answer?
Add Answer to:
Write in Java! Do NOT write two different programs for Deck and Card, it should be...
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
  • Answer the following questions and use Excel to show your work. A standard deck of playing...

    Answer the following questions and use Excel to show your work. A standard deck of playing cards consists of 52 cards. The cards in each deck consist of 4 suits, namely spades (♠), clubs (♣), diamonds (♦), and hearts (♥). Each suit consists of 13 cards, namely ace, king, queen, jack, 10, 9, 8, 7, 6, 5, 4, 3, and 2. In the game of poker, a royal flush consists of the ace, king, queen, jack, and 10 of the...

  • 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....

  • A standard 52-card deck has four 13-card suits: diamonds, hearts, 13-card suit contains cards numbered f...

    A standard 52-card deck has four 13-card suits: diamonds, hearts, 13-card suit contains cards numbered f probability of drawing a black king of hearts clubs, and spades. The diamonds and hearts are red, and the clubs and spades are black Each from 2 to 10, a jack, a queen, a king, and an ace. An experiment consists of drawing 1 card from the standard deck. Find the The probability of choosing a black king of hearts is ype an integer...

  • how many diamonds are in a deck of cards?

    A deck of cards contains 52 cards. They are divided into four suits: spades, diamonds, clubs and hearts. Each suit has 13 cards: ace through 10, and three picture cards: Jack, Queen, and King. Two suits are red in color: hearts and diamonds. Two suits are black in color: clubs and spades.Use this information to compute the probabilities asked for below and leave them in fraction form. All events are in the context that three cards are dealt from a...

  • There are 52 cards in a deck. 26 are red, and 26 are black. The 52...

    There are 52 cards in a deck. 26 are red, and 26 are black. The 52 cards make up four suits (hearts, diamonds, spades, clubs). There are 13 of each suit (ace-10, jack, queen, king). Essentially it is a fair deck of cards. a) What is the probability of drawing the 10 of clubs or a king, and then a spade? b) What is the probability of drawing a 7 or a heart, and then a 10 of hearts or...

  • Java programing Write a program that deals a deck of card into 4 hands – Each...

    Java programing Write a program that deals a deck of card into 4 hands – Each hand having 13 cards. The program should be doing the followings use an array to randomly select 52 cards and deal it into 4 hands. Print each hand unsorted Identify the face value of each hand and display it. You should create a class called Card, and all the methods should be defined within the card class. Hints – Import below java utility to...

  • Consider a standard 52-card deck of cards with 13 card values (Ace, King, Queen, Jack, and...

    Consider a standard 52-card deck of cards with 13 card values (Ace, King, Queen, Jack, and 2-10) in each of the four suits (clubs, diamonds, hearts, spades). If a card is drawn at random, what is the probability that it is a spade or a two? Note that "or" in this question refers to inclusive, not exclusive, or.

  • Before each draw the deck is well shuffled and a single card randomly drawn. (Use 4...

    Before each draw the deck is well shuffled and a single card randomly drawn. (Use 4 decimals for all answers) A. What is the probability that the first card drawn is a face card (a Jack, a Queen, or a King)? B. What is the probability that the second card drawn is red? C. What is the probability that the first card drawn is a face-card AND the second card drawn is red? D. What is the probability that the...

  • determine: 1. P(A and B) 2. P(A or B) 3. P( B|A ) Suppose one card...

    determine: 1. P(A and B) 2. P(A or B) 3. P( B|A ) Suppose one card is selected at random from an ordinary deck of 52 playing cards. A standard deck of cards contains for suits: red hearts, red diamonds, black clubs, black spades. Each suite contains 13 cards: Ace, 2,3,4,5,6,7,8, 9, 10, Jack, Queen, King. The Jack, Queen, and King are also called Face Cards. Let A = event a jack is selected B = event a spade is...

  • Write a class named Card which will represent a card from a deck of cards. A...

    Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that randomly shuffles the cards 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