Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java: The deck class is a little bit more complex. You can see from the UML that it is an aggregation of PlayingCards, which means

Java: The deck class is a little bit more complex. You can see from the UML that it is an aggregation of PlayingCards, which means it has a has a relationship with the PlayingCard class. You can also see that it has an ArrayList of PlayingCards, which is where the aggregation appears.
Deck Fields
Do not statically initialize any non-constant fields.
NUM_CARDS
This field represents the number of cards in the deck, which is 52.
Since it is in SCREAMING_SNAKE_CASE, you can infer that it should be final.
Since it is underlined in the UML, it should also be static.
generator
This field is used to generate random numbers for the shuffle method.
Remember to import its type from the util package.
Only declare it. Do not statically initialize it.
deck
This field contains all of the PlayingCard objects in the deck.
Remember to import its type from the util package.
Only declare it. Do not statically initialize it.
Deck Methods
Deck: No-Arg
This constructor sets the generator field to a new Random object. The generator is used in the shuffle method. It then calls the initialize method.
Deck: One-Arg
This constructor also sets the generator field, but this time sets it to a new Random object that you pass the seed that was given to the constructor. This allows a user to replicate the outcome of the shuffle method by using the same seed in their own random number generator. After setting the generator, it also calls the initialize method.
initialize
This method sets the deck field to a new ArrayList containing every type of playing card. The deck should have the playing cards in order of Rank followed by Suit. For example, two of clubs, two of diamonds, two of hearts, two of spades, three of clubs, three of diamonds, etc...
I recommend using nested For-Each loops to accomplish this method. You may do it however you want as long as you use loops and do not hard-code every card that you are adding to the deck.
This method is public so that users can reset their deck if they want.
shuffle
This method moves all the cards in the deck field around to pseudo randomly arrange them.
Here is how the algorithm works
Loop, starting at the last index down to 1
choose a random number between 0 and the current index
swap the card at the current index with the card at the random index chosen.
To swap cards, use the following algorithm.
Consider two cards: A and B
Create a new card, T, and store A in it.
Store B in A (since we have saved the original A in T)
Store T in B (since T is what A used to be)
getCard
Returns a PlayingCard object at the provided index in the deck.
toString()
Returns a string representation of the deck.
This should return the output of each cards toString method, in order, each separated by a newline.
There should not be a trailing newline at the end.
You will need to loop through the deck and concatenate the toString output of each card onto a string.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions