In: Computer Science
Write a console application called PlayingCards that has 4 classes: PlayingCards, Deck, Cards, Card. This application will be able to create a deck of cards, shuffle it, sort it and print it.
Use the following class diagrams to code your application.
Use the following code for the PlayingCards class
public class PlayingCards{ public static void main(String[] args){ System.out.println("Playings Cards"); Deck deck = new Deck(); deck.create(); System.out.println("Sorted cards"); deck.sort(); deck.showCards(); System.out.println("Shuffled cards"); deck.shuffle(); deck.showCards(); } } |
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== public class Card { private String suit; private String face; public Card(String suit, String face) { this.suit = suit; this.face = face; } public String getSuit() { return suit; } public String getFace() { return face; } @Override public String toString() { return getFace() + " of " + getSuit(); } }
================================================================
public abstract class Cards { public abstract Card[] getCards(); }
================================================================
import java.util.Arrays; import java.util.Comparator; import java.util.Random;
public class Deck extends Cards { private Card[] cardDeck; public Deck() { cardDeck = new Card[52]; } public void create() { String suits[] = {"Hearts", "Diamonds", "Spades", "Clubs"}; String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; int index = 0; for (String suit : suits) { for (String value : values) { cardDeck[index++] = new Card(suit, value); } } } @Override public Card[] getCards() { return cardDeck; } public void sort() { Arrays.sort(cardDeck, new Comparator<Card>() { @Override public int compare(Card o1, Card o2) { if(o1.getSuit().compareTo(o2.getSuit())!=0){ return o1.getSuit().compareTo(o2.getSuit()); }else{ return o1.getFace().compareTo(o2.getFace()); } } }); } public void showCards() { for (Card card : cardDeck) { System.out.println(card); } } public void shuffle() { // shuffle the deck 1000 times Random random = new Random(); for (int count = 1; count <= 1000; count++) { int firstCardIndex = random.nextInt(cardDeck.length); int secondCardIndex = random.nextInt(cardDeck.length); // swap the cards at these two indices Card tempCard = cardDeck[firstCardIndex]; cardDeck[firstCardIndex] = cardDeck[secondCardIndex]; cardDeck[secondCardIndex] = tempCard; } } }
================================================================
class PlayingCards{ public static void main(String[] args) { System.out.println("Playing Cards"); Deck deck = new Deck(); deck.create(); System.out.println("Sorted Cards:"); deck.sort(); deck.showCards(); System.out.println("Shuffled Cards:"); deck.shuffle(); deck.showCards(); } }
================================================================