Question

In: Computer Science

Complete the method sortSuits(). This method takes one argument:a deck of cards (Stack<String> deck).It should return...

Complete the method sortSuits(). This method takes one argument:a deck of cards (Stack<String> deck).It should return an ArrayList of Stacks, each stack representing all the cards of one suit(i.e., if you have four suits -Spades,Hearts,Clubs,Diamonds, it should return ArrayLists of Spades,Hearts,Clubs,and Diamonds). The returned ArrayList should be in the same order as the SUITSarray. Use a loop to look at each card, determine its suit (methods such as peek()/pop()and String.contains()may be helpful), and move the card from the deck to the stack associated with its suit.

package L2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

public class CardSort {
  
   public static final String[] RANKS = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
   public static final String[] SUITS = {"Spades","Hearts","Clubs","Diamonds"};
  
   public static void main(String[] args) {

       /* DO NOT EDIT THE MAIN METHOD */
      
       //make a deck of cards, should have 52 cards
       Stack<String> deck = makeDeck();
       System.out.println("New deck: "+deck.size()+" cards");
       System.out.println(deck);
       System.out.println();
      
       //automatically shuffles the deck, don't need to code this
       Collections.shuffle(deck);
       System.out.println("Shuffled deck: "+deck.size()+" cards");
       System.out.println(deck);
       System.out.println();
      
       //create an ArrayList of Stacks, each representing all the cards of one suit
       ArrayList<Stack<String>> bySuit = sortSuits(deck);
       for(int i = 0; i < bySuit.size(); i++) {
           //each new stack should have 13 cards, all the same suit
           System.out.println(SUITS[i]+": "+bySuit.get(i).size()+" cards");
           System.out.println(bySuit.get(i));
           System.out.println();
       }
      
       //the deck should now be empty
       System.out.println("Deck is empty? "+deck.isEmpty()+" ("+deck.size()+" cards)");      
   }
  
   public static Stack<String> makeDeck() {
      
       /* YOUR CODE HERE */
       int n = SUITS.length * RANKS.length;
       String[] deck = new String[n];
       for (int i = 0; i < RANKS.length; i++) {
           for (int j = 0; j < SUITS.length; j++) {
               deck[SUITS.length*i + j] = RANKS[i] + " of " + SUITS[j];
           }

       } Stack<String> stack2 = new Stack<String>();
       for(String text:deck) { stack2.push(text);
       }
       return (Stack<String>) stack2 ;
  
   }
  
   public static ArrayList<Stack<String>> sortSuits(Stack<String> deck) {
       return null;

  
   }
}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: The returned list of stacks are only arranged according to their suits. Cards in each suiit are not sorted.


// CardSort.java

package L2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

public class CardSort {

        public static final String[] RANKS = { "Ace", "2", "3", "4", "5", "6", "7",
                        "8", "9", "10", "Jack", "Queen", "King" };
        public static final String[] SUITS = { "Spades", "Hearts", "Clubs",
                        "Diamonds" };

        public static void main(String[] args) {

                /* DO NOT EDIT THE MAIN METHOD */

                // make a deck of cards, should have 52 cards
                Stack<String> deck = makeDeck();
                System.out.println("New deck: " + deck.size() + " cards");
                System.out.println(deck);
                System.out.println();

                // automatically shuffles the deck, don't need to code this
                Collections.shuffle(deck);
                System.out.println("Shuffled deck: " + deck.size() + " cards");
                System.out.println(deck);
                System.out.println();

                // create an ArrayList of Stacks, each representing all the cards of one
                // suit
                ArrayList<Stack<String>> bySuit = sortSuits(deck);
                for (int i = 0; i < bySuit.size(); i++) {
                        // each new stack should have 13 cards, all the same suit
                        System.out.println(SUITS[i] + ": " + bySuit.get(i).size()
                                        + " cards");
                        System.out.println(bySuit.get(i));
                        System.out.println();
                }

                // the deck should now be empty
                System.out.println("Deck is empty? " + deck.isEmpty() + " ("
                                + deck.size() + " cards)");
        }

        public static Stack<String> makeDeck() {

                /* YOUR CODE HERE */
                int n = SUITS.length * RANKS.length;
                String[] deck = new String[n];
                for (int i = 0; i < RANKS.length; i++) {
                        for (int j = 0; j < SUITS.length; j++) {
                                deck[SUITS.length * i + j] = RANKS[i] + " of " + SUITS[j];
                        }

                }
                Stack<String> stack2 = new Stack<String>();
                for (String text : deck) {
                        stack2.push(text);
                }
                return (Stack<String>) stack2;

        }

        public static ArrayList<Stack<String>> sortSuits(Stack<String> deck) {
                // creating a stacks array, with same length as SUITS array
                Stack<String> stacks[] = new Stack[SUITS.length];
                // looping until deck is empty
                while (!deck.isEmpty()) {
                        // popping top card
                        String card = deck.pop();
                        // looping through each suit name
                        for (int i = 0; i < SUITS.length; i++) {
                                // checking if card contains current suit name
                                if (card.contains(SUITS[i])) {
                                        // if stack at index i is null, initializing stack
                                        if (stacks[i] == null) {
                                                stacks[i] = new Stack<String>();
                                        }
                                        // adding card to stack at index i
                                        stacks[i].push(card);
                                }
                        }
                }
                // now creating an array list of stack of strings
                ArrayList<Stack<String>> list = new ArrayList<Stack<String>>();
                // looping and adding each non null stack from stacks array into the
                // list
                for (int i = 0; i < stacks.length; i++) {
                        if (stacks[i] != null) {
                                list.add(stacks[i]);
                        }
                }
                // returning list
                return list;

        }
}

/*OUTPUT*/

New deck: 52 cards
[Ace of Spades, Ace of Hearts, Ace of Clubs, Ace of Diamonds, 2 of Spades, 2 of Hearts, 2 of Clubs, 2 of Diamonds, 3 of Spades, 3 of Hearts, 3 of Clubs, 3 of Diamonds, 4 of Spades, 4 of Hearts, 4 of Clubs, 4 of Diamonds, 5 of Spades, 5 of Hearts, 5 of Clubs, 5 of Diamonds, 6 of Spades, 6 of Hearts, 6 of Clubs, 6 of Diamonds, 7 of Spades, 7 of Hearts, 7 of Clubs, 7 of Diamonds, 8 of Spades, 8 of Hearts, 8 of Clubs, 8 of Diamonds, 9 of Spades, 9 of Hearts, 9 of Clubs, 9 of Diamonds, 10 of Spades, 10 of Hearts, 10 of Clubs, 10 of Diamonds, Jack of Spades, Jack of Hearts, Jack of Clubs, Jack of Diamonds, Queen of Spades, Queen of Hearts, Queen of Clubs, Queen of Diamonds, King of Spades, King of Hearts, King of Clubs, King of Diamonds]

Shuffled deck: 52 cards
[6 of Diamonds, Ace of Clubs, 5 of Diamonds, Ace of Diamonds, Ace of Hearts, 7 of Hearts, Queen of Diamonds, Ace of Spades, 8 of Clubs, King of Clubs, 8 of Diamonds, King of Hearts, 5 of Spades, 5 of Clubs, 2 of Hearts, Queen of Hearts, 4 of Hearts, 7 of Spades, 7 of Clubs, 6 of Spades, King of Spades, 8 of Hearts, 5 of Hearts, 8 of Spades, 2 of Clubs, 6 of Clubs, Jack of Hearts, King of Diamonds, 10 of Diamonds, 3 of Diamonds, 2 of Diamonds, Jack of Clubs, 3 of Hearts, 4 of Spades, 7 of Diamonds, 10 of Clubs, Queen of Spades, 9 of Spades, 10 of Spades, 10 of Hearts, 2 of Spades, 3 of Clubs, 9 of Diamonds, Jack of Spades, 4 of Diamonds, Queen of Clubs, 3 of Spades, 4 of Clubs, Jack of Diamonds, 9 of Clubs, 6 of Hearts, 9 of Hearts]

Spades: 13 cards
[3 of Spades, Jack of Spades, 2 of Spades, 10 of Spades, 9 of Spades, Queen of Spades, 4 of Spades, 8 of Spades, King of Spades, 6 of Spades, 7 of Spades, 5 of Spades, Ace of Spades]

Hearts: 13 cards
[9 of Hearts, 6 of Hearts, 10 of Hearts, 3 of Hearts, Jack of Hearts, 5 of Hearts, 8 of Hearts, 4 of Hearts, Queen of Hearts, 2 of Hearts, King of Hearts, 7 of Hearts, Ace of Hearts]

Clubs: 13 cards
[9 of Clubs, 4 of Clubs, Queen of Clubs, 3 of Clubs, 10 of Clubs, Jack of Clubs, 6 of Clubs, 2 of Clubs, 7 of Clubs, 5 of Clubs, King of Clubs, 8 of Clubs, Ace of Clubs]

Diamonds: 13 cards
[Jack of Diamonds, 4 of Diamonds, 9 of Diamonds, 7 of Diamonds, 2 of Diamonds, 3 of Diamonds, 10 of Diamonds, King of Diamonds, 8 of Diamonds, Queen of Diamonds, Ace of Diamonds, 5 of Diamonds, 6 of Diamonds]

Deck is empty? true (0 cards)

Related Solutions

Create a method called firstLetter that takes a String parameter and integer parameter. It should return...
Create a method called firstLetter that takes a String parameter and integer parameter. It should return -1 if the number of words in the given String is greater than or equal to the integer parameter (it should return -1 and not process the String any further) (4 points). If the String does not have more words than the given integer, the method should return 1 if all the words in the string start with the same letter(8 points) and 0...
A standard deck of cards contains 52 cards. One card is selected from the deck. (a)...
A standard deck of cards contains 52 cards. One card is selected from the deck. (a) Compute the probability of randomly selecting a jack or ace. ​(b) Compute the probability of randomly selecting a jack or ace or nine. ​(c) Compute the probability of randomly selecting a king or diamond.
Write a method that takes four strings as parameter. The first string should be a pokemon...
Write a method that takes four strings as parameter. The first string should be a pokemon name, the second a pokemon type, the third a pokemon name, and the fourth a pokemon type. The method should print out which pokemon has the advantage over the other based on their type. In this case you can use fire, water, and leaf. Example outputs should be like: Example: Pokemon X (Water) has the advantage over Pokemon Y (fire) Pokemon X (Fire) has...
Java- creat a method that takes two char parameters. Return a String containing all characters, in...
Java- creat a method that takes two char parameters. Return a String containing all characters, in order, from the first char parameter (inclusive) to the last (inclusive). For instance, input('a', 'e'), and return "abcde".
In an application write a method filterStack that takes a stack of integers as a parameter...
In an application write a method filterStack that takes a stack of integers as a parameter and filters its elements (in a new Stack) in a way that places the even elements at the bottom and the odd ones at the top. The original stack should remain unchanged. You should use a queue (only one queue) as a temporary storage. Use stack and queue operations only to solve this problem. No need to write the main method. For example, if...
Cards: Suppose you draw one card from a single deck of cards. (a) What is the...
Cards: Suppose you draw one card from a single deck of cards. (a) What is the probability that you draw an queen? Round your answer to 3 significant digits*. (b) What is the probability that you draw a heart? Round your answer to 3 significant digits*. (c) What is the probability that you draw the queen of hearts? Round your answer to 3 significant digits*. ............................................... *Significant Digits: Here are some probabilities expressed to 3 significant digits. You start counting...
Two cards are drawn one after the other from a standard deck of 52 cards. (a)...
Two cards are drawn one after the other from a standard deck of 52 cards. (a) In how many ways can one draw first a spade and then a heart? (b) In how many ways can one draw first a spade and then a heart or a diamond? (c) In how many ways can one draw first a spade and then another spade? (d) Do the previous answers change if the first card is put back in the deck before...
Find s shuffle of a deck of 13 cards that requires 42 repeats to return to...
Find s shuffle of a deck of 13 cards that requires 42 repeats to return to the original order. How about one requiring 30 repeats?
public int static mirroringIt(String str){ return n; } implement this function so its takes ONE string...
public int static mirroringIt(String str){ return n; } implement this function so its takes ONE string and remove the characters so it becomes a mirrored word. example: Input: qswwbi Output: 4 remove q s b i "wow" is a mirror word. output : 0 Input : "if" output : 1 remove either I or f because it's two words. don't use 3rd parties library or java.util.
In this game, there is only one deck of cards. You play with a friend and...
In this game, there is only one deck of cards. You play with a friend and the deck of cards belongs to him/her. Numbered cards are worth their face value, jacks are worth 11, queen 12, kings 13 and aces 14. You have a suspicion that in this deck of cards, your friend has replaced some high cards in the deck with low cards. You take 10 cards and quickly calculate the average value: 4.5. You do the math: In...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT