Question: The code is correct, but it needs to be a little differently set up. It needs to have a DealOneCard() constructor which is supposed to

The code is correct, but it needs to be a little differently set up. It needs to have a DealOneCard() constructor which is supposed to be the method that deals one card at a time.Below is my code for java deck of cards program. The objective is to have one random card displayed which is the first card, and be able to display 52 cards. So for example the first card is displayed, then ok is pressed in GUI and then second card is displayed.. and so on, and user can get 52 random cards to show. If the user tries to get to 53 cards there should be an error message. I need some help on doing that.

import java.util.ArrayList;

import javax.swing.JOptionPane;

public class Cards {

public static void main(String[] args) {

int[] deck = new int[52];

String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" };

String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };

ArrayList shuffledDeck = new ArrayList(0);

ArrayList randArr = new ArrayList(0);

for (int i = 0; i < deck.length; i++)

deck[i] = i;

for (int i = 0; i < deck.length; i++) {

int shuffle = (int) (Math.random() * deck.length);

while (randArr.contains(shuffle)) {

shuffle = (int) (Math.random() * deck.length);

}

randArr.add(shuffle);

shuffle(Math.random() * 100);

int temp = deck[i];

deck[i] = deck[shuffle];

deck[shuffle] = temp;

}

int decknum = 0;

// Error message

String errorMesg = "Only 52 cards in deck";

for (int i = 0; i < 52; i++) {

String suit = suits[deck[i] / 13];

String rank = ranks[deck[i] % 13];

decknum++;

// String variable which will contain card number with card name

// This will be shown in GUI.

String message = "card number " + decknum + ": " + rank + " of " + suit;

// This message will be printed on terminal

System.out.println(message);

// This will show the message in GUI

JOptionPane.showMessageDialog(null, message);

// It will suffle the deck cards

shuffledDeck.add(rank + "of" + suit);

}

/* Condition to show error message */

if (decknum >= 52) {

JOptionPane.showMessageDialog(null, errorMesg);

}

}

private static ArrayList shuffle(double d) {

return null;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!