Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to create a class Deck. I already created the class Card. The Deck class also contains the following public methods: (18 points) Write

I need to create a class Deck. I already created the class Card.

The Deck class also contains the following public methods:

(18 points) Write a class Deck. A Deck has the following private attribute:

A constructor that takes nothing as input and initializes the attribute with a Card[] of size 52. The constructor should also initialize such array with all 52 possible cards. To get full marks you must use at least 1 loop to accomplish this (that is, you cannot write 52 total statements to assign all possible values). Hint: Create a String array of size 4 with the suit values, and then use nested for loops to initialize the cards.

shuffle() which takes no input and returns no value. This method should shuffle the array of Cards of this Deck. To do so, do the following:

create an object of type Random (remember that to use Random you should add the appro- priate import statement at the beginning of your file). To make your program easier to debug (and grade), you must provide a seed for the Random object. Use a seed equal to 123.

Then create a loop that iterates 1000 times.

Inside the loop, generate 2 random integers between 0 and 51 (both included) and swap

the values at those positions in the array.

For example, suppose the array of Cards of this Deck is the following:

 [AH, 2H, 3H, 4H, 5H, 6H, 7H, 8H, 9H, 10H, JH, QH, KH, AS, 2S, 3S, 4S, 5S, 6S, 7S, 8S, 9S, 10S, JS, QS, KS, AC, 2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, 10C, JC, QC, KC, AD, 2D, 3D, 4D, 5D, 6D, 7D, 8D, 9D, 10D, JD, QD, KD] 

Where the first symbol denotes the value of the card and the second the suit (C stands for clubs, H for hearts, D for diamonds, and S for spades). Then, after the method shuffle() is appropriately called on such Deck, the array of Cards will look as follows:

 [AD, 6D, AS, QH, 7H, 5D, 5S, KS, 9D, AH, 4D, 6H, 4H, 2H, 9S, 10D, JH, JD, KD, 8H, 3C, 7S, 10C, 8S, JC, 10H, QS, 4C, 3H, 9H, 7D, 8C, KH, JS, 3S, 7C, 6C, 3D, 9C, 6S, QC, 10S, 5H, 5C, 4S, 2D, KC, 8D, 2S, 2C, QD, AC] 

dealHand() which takes as input two integers. The first one, call it n, indicates how many cards should be dealt, the second one indicates the players number. The idea is that the first player should be dealt the first n cards in this Deck, the second player the next n cards, and so on. The method should return an array of Cards representing the hand dealt to the player. If there are not enough cards to deal (for instance we cannot deal a hand of 12 cards to a fifth player, since the first four players already took 48 cards, and only 4 cards are left in the deck), then the method should throw an IllegalArgumentException explaining that there are not enough cards in the deck.

What I did:

import java.util.Random;

public class Deck {

// Attribute of the Card class

public Card[] arrayOfCard;

// Constructor that takes nothing as input and initializes the private attribute

// of Card[]

public Deck() {

// Setting the arrayofCard of size 52

arrayOfCard = new Card[52];

// Setting the arrayOfCard the 4 suits.

String[] suits = { "spades", "hearts", "diamonds", "clubs" };

int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

int x = 0;

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

for (int j = 0; j < values.length; j++) {

arrayOfCard[x] = new Card(suits[i], values[j]);

x++;

}

}

}

public Card[] getDeck() {

return arrayOfCard;

}

public void setDeck(Card[] theArray) {

this.arrayOfCard = theArray;

}

public void Shuffle() {

Random rand = new Random(123);

int temporary = rand.nextInt(52);

Card temporaryHolder;

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

temporaryHolder = arrayOfCard[rand.nextInt(52)];

arrayOfCard[rand.nextInt(52)] = arrayOfCard[temporary];

arrayOfCard[temporary] = temporaryHolder;

}

}

public Card[] dealHand(int n, int playerNumber) {

if (playerNumber * n > 52) {

throw new IllegalArgumentException("There are not enough cards in the deck for this number of players");

}

for (int i = (n * playerNumber) - n; i < n * playerNumber; i++) {

arrayOfCard[i] = arrayOfCard[n];

}

return arrayOfCard;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions