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

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...
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...
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.
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?
Time String to Seconds Write a method that takes a string representation of time in hours:minutes:seconds...
Time String to Seconds Write a method that takes a string representation of time in hours:minutes:seconds and returns the total number of seconds For example, if the input is “0:02:20” it would return 140 If the input string is missing the hours component it should still work For example, input of “10:45” would return 645 Write an overloaded form of the method that takes in only hours and minutes and returns the total number of seconds It should leverage the...
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...
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
A game comes with a deck of cards with each card showing one of the numbers...
A game comes with a deck of cards with each card showing one of the numbers 2, 3, 4, 5, 6, or 8. Each card occurs one time in red, one time in purple, and one time in blue. Given that a blue card is drawn, what is the probability the card is a 4?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT