I need help creating a class Stack for my blackjack program. I need to create a
ID: 3657645 • Letter: I
Question
I need help creating a class Stack for my blackjack program. I need to create a standard set of cards [array], be able to shuffle, add_to_stack, and remove_from_stack. This is what I have so far. package game; public class Stack { int cards_per_deck = 52; String spade="u2660", heart = "u2665" , diamond = "u2666", club = "u2663"; Card [] cards = new Card[52]; for(int i=0; i < cards_per_deck; i++) { cards[i] = new Card(); if(i < 13) { cards[i].pipValue = i; cards[i].suit = spade; } else if (i >= 13 && i < 26) { cards[i].pipValue = i-13; cards[i].suit = heart; } else if (i >= 26 && i < 39) { cards[i].pipValue = i-26; cards[i].suit = diamond; } else { cards[i].pipValue = i-39; cards[i].suit = club; } } public void shuffle(Stack card) { } public void add_to_stack(Stack card) { } public void remove_from_stack(Stack card) { } } //0-12, 13-25, 26-38, 39-51Explanation / Answer
Use an ArrayList for implementation, it's much easier. It will take care of the array resizing for you.
import java.util.ArrayList;
public class Deck
{
private ArrayList<Card> cards;
public Deck()
{
cards = new ArrayList<Card>();
}
public void shuffle()
{
Collections.shuffle(cards);
}
public boolean isEmpty()
{
return cards.isEmpty();
}
public void add_to_stack(Card c)
{
cards.add(c);
}
public Card remove_from_stack()
{
return cards.remove(cards.size()-1);
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.