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

60.1K

Verified Solution

Question

Programming

Complete the method sortSuits(). This method takes oneargument:a deck of cards (Stack deck).It shouldreturn an ArrayList of Stacks, each stack representing all thecards of one suit(i.e., if you have four suits-Spades,Hearts,Clubs,Diamonds, it should return ArrayLists ofSpades,Hearts,Clubs,and Diamonds). The returned ArrayList should bein the same order as the SUITSarray. Use a loop to look at eachcard, determine its suit (methods such as peek()/pop()andString.contains()may be helpful), and move the card from the deckto 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 have52 cards
       Stack 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> bySuit =sortSuits(deck);
       for(int i = 0; i            //each new stackshould 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 beempty
       System.out.println(\"Deck is empty?\"+deck.isEmpty()+\" (\"+deck.size()+\" cards)\");     
   }
  
   public static Stack makeDeck() {
      
       /* YOUR CODE HERE */
       int n = SUITS.length *RANKS.length;
       String[] deck = newString[n];
       for (int i = 0; i            for (int j = 0;j < SUITS.length; j++) {
              deck[SUITS.length*i + j] = RANKS[i] + \" of \" +SUITS[j];
           }

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

  
   }
}

Answer & Explanation Solved by verified expert
4.3 Ratings (885 Votes)
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 Ill 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 CardSortjava package L2 import javautilArrayList import javautilCollections import javautilList import javautilStack 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 mainString args DO NOT EDIT THE MAIN METHOD make a deck of cards should have 52 cards Stack deck makeDeck SystemoutprintlnNew deck decksize cards Systemoutprintlndeck Systemoutprintln automatically    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students