Answered step by step
Verified Expert Solution
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 nonconstant fields. NUMCARDS This field represents the number of cards in the deck, which is Since it is in SCREAMINGSNAKECASE, 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: NoArg 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: OneArg 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 ForEach loops to accomplish this method. You may do it however you want as long as you use loops and do not hardcode 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 choose a random number between 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.
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 nonconstant fields.
NUMCARDS
This field represents the number of cards in the deck, which is
Since it is in SCREAMINGSNAKECASE, 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: NoArg
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: OneArg
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 ForEach loops to accomplish this method. You may do it however you want as long as you use loops and do not hardcode 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
choose a random number between 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
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started