In: Computer Science
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;
}
}
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)