Question

In: Computer Science

Using Java, design a program that creates an array of Card objects, shuffles them, then recursively...

Using Java, design a program that creates an array of Card objects, shuffles them, then recursively sorts them by value and then recursively sorts them by value and suit



You will need to create a Card class with the following two fields:
• value (a String)
• suit (a String)
You may include any other fields, constructors, or methods as needed.
Your single array will need to contain 52 card objects, one for each value of each suit. The values to use are 2-10, J, Q, K, and A, with A being the card with the highest value. The suit values to use are H (for Hearts), D (for Diamonds), C (for Clubs), and S (for Spades).
After building the array, shuffle the array using the Fisher-Yates Algorithm. You must implement this algorithm yourself. After shuffling, print each object’s value and suit to show the array was shuffled. The output for each object printed should be VALUE - SUIT. For example: 2 - H.
Then, you’ll need to recursively sort the array based on the values. All 2 cards should be at the beginning and all A cards should be at the end; The suit is irrelevant here. After sorting, print each object’s value and suit again to show the array was sorted based on the cards’ values.Finally, you’ll recursively sort the array based on the values and suit. Clubs should be first, then Diamonds, then Hearts, then Spades. Each suit should be sorted from 2-A. After sorting, print each object’s value and suit again to show the array was sorted based on the cards’ values and suits.


please comment

Solutions

Expert Solution

Thanks for the question, here are the two classes - Card and Deck. Deck is the driver class, Deck class create an array fof cards, shuffles the cards using the Fisher-Yates algorithm and then recursively sorts the array using bubble sort alogrithm . It prints both the shuffled and sorted cards.

Here you go !

===========================================================

public class Card implements Comparable<Card> {


    private String value;
    private String suit;

    public Card(String val, String suit) {
        value = val;
        this.suit = suit;
    }

    public String getSuit() {
        return suit;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return getValue() + " - " + getSuit();
    }

    @Override
    public int compareTo(Card card) {

        return cardRanking(this) - cardRanking(card);
    }

// returns the card value club 2 has ranking 1 while card Spades - A has value 52
    private static int cardRanking(Card card) {

        String cards[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        String suits[] = {"C", "D", "H", "S"};

        int rank = 0;
        for (int i = 0; i < cards.length; i++) {
            if (cards[i].equals(card.getValue())) {
                rank = i + 1;
                break;
            }
        }
        int suitRank = 0;
        for (int i = 0; i < suits.length; i++) {
            if (suits[i].equals(card.getSuit())) {
                suitRank = i+1;
                break;
            }
        }

        return (suitRank * 13) + rank;
    }
}

=============================================================

import java.util.Random;

public class Deck {


    public static void main(String[] args) {

        String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        String suits[] = {"C", "D", "H", "S"};


// create an array of Card object
        Card deckOfCard[] = new Card[values.length * suits.length];
        int index = 0;
        for (String value : values) {
            for (String suit : suits) {
                deckOfCard[index++] = new Card(value, suit);
            }
        }
        shuffle(deckOfCard);
        System.out.println("Printing the shuffled deck of cards - ");
        displayDeckOfCards(deckOfCard);
        recusriveBubbleSort(deckOfCard,deckOfCard.length);
        System.out.println("Printing the sorted deck of cards - ");
        displayDeckOfCards(deckOfCard);

    }

    private static void displayDeckOfCards(Card[] deckOfCard) {

        for (Card card : deckOfCard) {
            System.out.println(card);
        }
    }

    private static void shuffle(Card[] cards) {

        // shuffle using fisher yates algorithm
        Random random = new Random();
        for (int i = 0; i < cards.length - 1; i++) {
            int j = i + random.nextInt(cards.length - i);
            Card temp = cards[i];
            cards[i] = cards[j];
            cards[j] = temp;

        }
    }

// recursively sorts the array of cards
    private static void recusriveBubbleSort(Card cards[], int n) {
        if (n == 1) return;
        for (int i = 0; i < n - 1; i++) {
            if (cards[i].compareTo(cards[i + 1]) > 0){
                Card temp = cards[i];
                cards[i] = cards[i + 1];
                cards[i + 1] = temp;
            }
        }
        recusriveBubbleSort(cards,n-1);
    }
}

========================================================


Related Solutions

Write a java program that creates a hashtable with 10 objects and 5 data members using...
Write a java program that creates a hashtable with 10 objects and 5 data members using mutator and accessor methods for each data member.
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
java please Write a program that creates an ArrayList and adds 5 circle objects to the...
java please Write a program that creates an ArrayList and adds 5 circle objects to the list , and display all elements in the list by invoking the object’s toString() method.
Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Learning Outcomes Using Java, maintain a collection of objects using an array. Construct a class that...
Learning Outcomes Using Java, maintain a collection of objects using an array. Construct a class that contains an array as a private instance variable. Construct methods with arrays as parameters and return values. Use partially filled arrays to implement a class where objects can be dynamically added. Implement searching and sorting algorithms. Instructions For this assignment you will be implementing an application that manages a music collection. The application will allow the user to add albums to the collection and...
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
java program Create a program that creates and prints a random phone number using the following...
java program Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes.  Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint:   Think though the easiest way to construct the phone number. Each digit does do not have to be determined...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT