Write the following Java program that uses concepts of collection/stream API and
ID: 3695788 • Letter: W
Question
Write the following Java program that uses concepts of collection/stream API and linked lists (comment code throughly)
1. A "card" class that stores the suit and face value for an individual card
2. A "deck" class that contains a list of the 52 card objects
- Contains a shuffle method that randomly shuffles the cards
- Must have deal method. that deals a card by removing the Card object at the beginning of the list and returns a reference to that object (i.e. the Card object).
3. A "cardPlayer" class that keeps a list of cards that were dealt
- getCard method that gets passed to it a reference to a Card object and adds it to the list (maintained by CardPlayer).
-showCards method that displays all of the card objects in the list
This program should (Summary):
a. Create a deck object
b. Shuffle the cards it contains
c. Deal 5 cards from the Deck to a CardPlayer object
d. Displays the cards in the CardPlayer object using the showCards method
Explanation / Answer
Make normal 52-card deck.
string[] suit=new string[]{ "spades","hearts","diamonds","club"};
string[] ranks=new string[]{"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"};
list<card>deck=new arraylist<card>();
for(int p=0;p<suit.length;p++)
{
for(int q=0;q<rank.length;q++)
{
deck.add(rank[q])+"of"+suit[p]);
}
}
shuffling the deck
collections.shuffle(deck); //calling the library version of shuffle ,
which uses a default sources of randomness//
if(numhands*cardsperhand>decsize())
{
system.out.println("not enouugh of cards");
return;
}
for(int p=0;p<numhands;p++)
system.out.println(dealhand(deck,cardsperhand));
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.